Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generate shorter UUIDs (base64 encoded format) #692

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Core/XMPPStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -712,8 +712,11 @@ extern const NSTimeInterval XMPPStreamTimeoutNone;
* UUIDs (Universally Unique Identifiers) may also be known as GUIDs (Globally Unique Identifiers).
*
* The UUID is generated using the CFUUID library, which generates a unique 128 bit value.
* The uuid is then translated into a string using the standard format for UUIDs:
* "68753A44-4D6F-1226-9C60-0050E4C00067"
* It is then translated into a 22 character string using its URL friendly
* base64 encoded format: without padding (=) and replacing the following characters
* ('+' -> '-') and ('/' -> '_')
*
* e.g. "GHpy_-yfSf--It29T31fhw" (from base64 form: GHpy/+yfSf++It29T31fhw==)
*
* This method is most commonly used to generate a unique id value for an xmpp element.
**/
Expand Down
31 changes: 22 additions & 9 deletions Core/XMPPStream.m
Original file line number Diff line number Diff line change
Expand Up @@ -5001,15 +5001,28 @@ - (void)enumerateModulesOfClass:(Class)aClass withBlock:(void (^)(XMPPModule *mo

+ (NSString *)generateUUID
{
NSString *result = nil;

CFUUIDRef uuid = CFUUIDCreate(NULL);
if (uuid)
{
result = (__bridge_transfer NSString *)CFUUIDCreateString(NULL, uuid);
CFRelease(uuid);
}

CFUUIDRef uuid = CFUUIDCreate(NULL);
if (!uuid) return nil;

CFUUIDBytes bytes = CFUUIDGetUUIDBytes(uuid);
CFRelease((CFTypeRef)uuid);

NSData *data = [NSData dataWithBytes:&bytes length:sizeof(bytes)];
NSString *base64;

if ([data respondsToSelector:@selector(base64EncodedStringWithOptions:)])
{
base64 = [data base64EncodedStringWithOptions:0]; // iOS 7+
}
else
{
base64 = [data base64Encoding]; // pre iOS7
}

NSString *result = [[[base64
stringByReplacingOccurrencesOfString:@"/" withString:@"_"]
stringByReplacingOccurrencesOfString:@"+" withString:@"-"]
stringByReplacingOccurrencesOfString:@"=" withString:@""];
return result;
}

Expand Down