Objective C: SHA1

前端 未结 3 816
粉色の甜心
粉色の甜心 2020-11-29 01:36

How do i sha1 a string or set of numbers in Objective c?

3条回答
  •  清酒与你
    2020-11-29 02:16

    Another solution with a message digest library (nv-ios-digest):

    (1) String

    // Create an SHA1 instance, update it with a string and do final.
    SHA1 sha1 = [SHA1 sha1WithString:@"Hello"];
    
    // Get the pointer of the internal buffer that holds the message digest value.
    // The life of the internal buffer ends when the SHA1 instance is discarded.
    // Copy the buffer as necessary. The size of the buffer can be obtained by
    // 'bufferSize' method.
    unsigned char *digestAsBytes = [sha1 buffer];
    
    // Get the string expression of the message digest value.
    NSString *digestAsString = [sha1 description];
    

    (2) Numbers

    // Create an SHA1 instance.
    SHA1 sha1 = [[SHA1 alloc] init];
    
    // Update the SHA1 instance with numbers.
    // (Sorry, the current implementation is endianness-dependent.)
    [sha1 updateWithShort:(short)1];
    [sha1 updateWithInt:(int)2];
    [sha1 updateWithLong:(long)3];
    [sha1 updateWithLongLong:(long long)4];
    [sha1 updateWithFloat:(float)5];
    [sha1 updateWithDouble:(double)6];
    
    // Do final. 'final' method returns the pointer of the internal buffer
    // that holds the message digest value. 'buffer' method returns the same.
    // The life of the internal buffer ends when the SHA1 instance is discarded.
    // Copy the buffer as necessary. The size of the buffer can be obtained by
    // 'bufferSize' method.
    unsigned char *digestAsBytes = [sha1 final];
    
    // Get the string expression of the message digest value.
    NSString *digestAsString = [sha1 description];
    

    The message digest library supports MD5, SHA-1, SHA-224, SHA-256, SHA-384 and SHA-512.

    [Blog] Message digests (MD5, SHA1, etc.) on iOS with dedicated classes
    http://darutk-oboegaki.blogspot.jp/2013/04/message-digests-md5-sha1-etc-on-ios.html

    [Library] nv-ios-digest
    https://github.com/TakahikoKawasaki/nv-ios-digest

提交回复
热议问题