Skip to content

Commit 088abb9

Browse files
committed
-[SSYUUID compactUuid] now uses character set recommended by RFC4648.
1 parent f03ea7a commit 088abb9

File tree

2 files changed

+37
-21
lines changed

2 files changed

+37
-21
lines changed

SSYUuid.h

+13-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,19 @@
1818
+ (NSString*)uuid ;
1919

2020
/*!
21-
@brief Returns a new UUID string represented as 22 base 64 characters
22-
(except with "/" replaced by "-").
21+
@brief Returns a new UUID string represented as 22 base64url characters,
22+
23+
@details Note that base64url, defined in RFC 4648, differs from conventional
24+
Base64 in that uses '-' and '_' instead of '/' and '+' as the 63rd and 64th
25+
characters. This is because these are more "URL friendly"…
26+
* https://tools.ietf.org/html/rfc4648#section-5
27+
28+
This character set has also been adopted by Mozilla for bookmark GUID in
29+
Firefox:
30+
* https://bugzilla.mozilla.org/show_bug.cgi?id=607115 (Comment 2010-11-22 11:19:21 PST)
31+
32+
Prior to Novemeber 2016, this method used '-' and '+' as the 63rd and 64th
33+
characters.
2334
*/
2435
+ (NSString*)compactUuid ;
2536

SSYUuid.m

+24-19
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,30 @@ + (NSString*)uuid {
4040
}
4141

4242
+ (NSString*)compactUuid {
43-
CFUUIDRef cfUUID = CFUUIDCreate(kCFAllocatorDefault) ;
44-
CFUUIDBytes uuidBytes = CFUUIDGetUUIDBytes(cfUUID) ;
45-
CFRelease(cfUUID) ;
46-
NSData* data = [NSData dataWithBytes:(const void*)&uuidBytes
47-
length:16] ;
48-
NSMutableString* s = [NSMutableString stringWithString:[data stringBase64Encoded]] ;
49-
50-
// Remove the padding at the end
51-
[s replaceOccurrencesOfString:@"="
52-
withString:@""
53-
options:0
54-
range:NSMakeRange(0, [s length])] ;
55-
56-
// Dashes are more readable than slashes
57-
[s replaceOccurrencesOfString:@"/"
58-
withString:@"-"
59-
options:0
60-
range:NSMakeRange(0, [s length])] ;
61-
return [NSString stringWithString:s] ;
43+
CFUUIDRef cfUUID = CFUUIDCreate(kCFAllocatorDefault) ;
44+
CFUUIDBytes uuidBytes = CFUUIDGetUUIDBytes(cfUUID) ;
45+
CFRelease(cfUUID) ;
46+
NSData* data = [NSData dataWithBytes:(const void*)&uuidBytes
47+
length:16] ;
48+
NSMutableString* s = [NSMutableString stringWithString:[data stringBase64Encoded]] ;
49+
50+
// Remove the padding at the end
51+
[s replaceOccurrencesOfString:@"="
52+
withString:@""
53+
options:0
54+
range:NSMakeRange(0, [s length])] ;
55+
56+
// Change from standard Base64 to base64url (RFC4648) character set
57+
[s replaceOccurrencesOfString:@"/"
58+
withString:@"-"
59+
options:0
60+
range:NSMakeRange(0, [s length])] ;
61+
[s replaceOccurrencesOfString:@"+"
62+
withString:@"_"
63+
options:0
64+
range:NSMakeRange(0, [s length])] ;
65+
66+
return [NSString stringWithString:s] ;
6267
}
6368

6469
@end

0 commit comments

Comments
 (0)