从名称空间生成UUID?

时间:2018-01-03 10:03:59

标签: ios swift uuid

尝试根据2个唯一字符串生成客户端ID。 这最终应该与服务器中生成的UUID相同,来自同一个Ids。

使用Javascript看起来像这样:

uuidv5(id1 + id2 , uuidv5.DNS);

似乎无法在Swift上找到生成此方法的方法,NSUUID只能从无生成中生成UUID

NSUUID().uuidString

我正在寻找类似的东西:

NSUUID(namespace: id1 + id2).uuidString

修改

示例:

let sorted = ["5a23dbfb2626b400190998fc", "5pCAvA7h8k9JuErRn"]
let appended = sorted.seperaredStringBy("-")
let result = uuidv5(appended , uuidv5.DNS)

//results:
2522b097-8532-548e-a18b-9366c6511b5e

2 个答案:

答案 0 :(得分:3)

据我所知,Swift标准库或Foundation框架没有版本5 UUID的内置方法。

以下是版本3和版本5 UUID的可能实现,取自Generating v5 UUID. What is name and namespace?中的说明和RFC 4122中的参考实现。

它使用CommonCrypto框架进行哈希计算,所以你 必须包括

var date = (new Date()).toString(); 

在桥接头文件中。

#import <CommonCrypto/CommonCrypto.h>

示例1(您的案例):

extension UUID {

    enum UUIDVersion: Int {
        case v3 = 3
        case v5 = 5
    }

    enum UUIDv5NameSpace: String {
        case dns  = "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
        case url  = "6ba7b811-9dad-11d1-80b4-00c04fd430c8"
        case oid  = "6ba7b812-9dad-11d1-80b4-00c04fd430c8"
        case x500 = "6ba7b814-9dad-11d1-80b4-00c04fd430c8"
    }

    init(version: UUIDVersion, name: String, nameSpace: UUIDv5NameSpace) {
        // Get UUID bytes from name space:
        var spaceUID = UUID(uuidString: nameSpace.rawValue)!.uuid
        var data = withUnsafePointer(to: &spaceUID) {
            Data(bytes: $0, count: MemoryLayout.size(ofValue: spaceUID))
        }

        // Append name string in UTF-8 encoding:
        data.append(contentsOf: name.utf8)

        // Compute digest (MD5 or SHA1, depending on the version):
        var digest = [UInt8](repeating: 0, count:Int(CC_SHA1_DIGEST_LENGTH))
        data.withUnsafeBytes { (ptr: UnsafePointer<UInt8>) -> Void in
            switch version {
            case .v3:
                _ = CC_MD5(ptr, CC_LONG(data.count), &digest)
            case .v5:
                _ = CC_SHA1(ptr, CC_LONG(data.count), &digest)
            }
        }

        // Set version bits:
        digest[6] &= 0x0F
        digest[6] |= UInt8(version.rawValue) << 4
        // Set variant bits:
        digest[8] &= 0x3F
        digest[8] |= 0x80

        // Create UUID from digest:
        self = NSUUID(uuidBytes: digest) as UUID
    }
}

示例2(来自RFC 4122中的附录B,已在Errata中更正):

let uuid = UUID(version: .v5, name: "5a23dbfb2626b400190998fc-5pCAvA7h8k9JuErRn", nameSpace: .dns)
print(uuid) // 2522B097-8532-548E-A18B-9366C6511B5E

答案 1 :(得分:0)

objc 版本,以防有人需要它。

+ (CBUUID *)UUIDWithNamespace:(NSString *)namespace name:(NSString *)name version:(int)version
{
    if (namespace.length <= 0 || name == nil  ) { return nil; }
    if (!(version == 3        || version == 5)) { return nil; }
    CBUUID *namespaceUUID = [CBUUID UUIDWithString:namespace];
    if (namespaceUUID == nil) { return nil; }
    NSMutableData *data = [[NSMutableData alloc] init];
    [data appendData:namespaceUUID.data];
    [data appendData:[name dataUsingEncoding:NSUTF8StringEncoding]];
    unsigned char hash[CC_SHA1_DIGEST_LENGTH];
    if (version == 3) {
        CC_MD5(data.bytes, (int)data.length, hash);
    } else if (version == 5) {
        CC_SHA1(data.bytes, (int)data.length, hash);
    }
    unsigned char result[16];
    memcpy(result, hash, 16);
    result[6] &= 0x0F;
    result[6] |= (((unsigned char)version) << 4);
    result[8] &= 0x3F;
    result[8] |= 0x80;
    return [CBUUID UUIDWithData:[NSData dataWithBytes:result length:16]];
}