Creating SHA1 Hash from NSString

2019-01-31 05:56发布

How can I create a SHA1 from a NSString.

Let's say the NSString is set up as:

NSString *message = @"Message";

I can use PHP to create a SHA1 hash with sha($message). But unfortunately it doesn't work like that within Objective-C.

8条回答
手持菜刀,她持情操
2楼-- · 2019-01-31 06:14

try this:

#import <CommonCrypto/CommonDigest.h>

-(NSData *) selector
{
    unsigned char hashBytes[CC_SHA1_DIGEST_LENGTH];
    CC_SHA1([dataToHash bytes], [dataToHash length], hashBytes);
    NSData *data = [[NSData alloc] initWithBytes:hashBytes length:CC_SHA1_DIGEST_LENGTH];
}
查看更多
forever°为你锁心
3楼-- · 2019-01-31 06:19

It took me a while to port @hypercrypt solution to Swift so I decided to share it with others that might have the same problem.

One important thing to note is that you need CommonCrypto library, but that library does not have Swift module. The easiest workaround is to import it in your bridging header:

#import <CommonCrypto/CommonCrypto.h>

Once imported there, you do not need anything else. Just use String extension provided:

extension String
{
    func sha1() -> String
    {
        var selfAsSha1 = ""

        if let data = self.dataUsingEncoding(NSUTF8StringEncoding)
        {
            var digest = [UInt8](count: Int(CC_SHA1_DIGEST_LENGTH), repeatedValue: 0)
            CC_SHA1(data.bytes, CC_LONG(data.length), &digest)

            for index in 0..<CC_SHA1_DIGEST_LENGTH
            {
                selfAsSha1 += String(format: "%02x", digest[Int(index)])
            }
        }

        return selfAsSha1
    }
}

Notice that my solution does not take effect of reserving capacity what NSMutableString has in original post. However I doubt anyone will see the difference :)

查看更多
We Are One
4楼-- · 2019-01-31 06:22

I quite like hypercrypt's answer, but I've been encouraged to post my comment.

You could look at CC_SHA1, or this related SO question.

查看更多
甜甜的少女心
5楼-- · 2019-01-31 06:23

I like hypercrypt's answer so much I packaged it into a little git repo. Check out the NSString category on Github.

Also feel free to add to it with any other good NSString Crypto

查看更多
兄弟一词,经得起流年.
6楼-- · 2019-01-31 06:27

I'm seeing a few different possible improvements to the answers in this post.

  1. Never make an unprefixed category. What if you implement -[NSString sha1Hash] in your library, and another library in the same app implements the same method with slightly different semantics? Which one is used will be random, and lead to hard-to-diagnose errors.
  2. If you want a string, there is base64-encoding in the standard library nowadays. Less work than manually building hex stringification.

Here's my solution, adapted from the excellent SocketRocket library's SRHash.m:

// NSString+Sha1Digest.h
#import <Foundation/Foundation.h>

@interface NSString (LBDigest)
- (NSString *)lb_digestString;
@end

@interface NSData (LBDigest)
- (NSString *)lb_digestString;
@end

// NSString+SHA1Digest.m
#import "NSString+Sha1Digest.h"
#import <CommonCrypto/CommonDigest.h>

static NSData *LBSHA1HashFromBytes(const char *bytes, size_t length)
{
    uint8_t outputLength = CC_SHA1_DIGEST_LENGTH;
    unsigned char output[outputLength];
    CC_SHA1(bytes, (CC_LONG)length, output);

    return [NSData dataWithBytes:output length:outputLength];
}

static NSData *LBSHA1HashFromString(NSString *string)
{
    size_t length = [string lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
    return LBSHA1HashFromBytes(string.UTF8String, length);
}

@implementation NSData (LBDigest)
- (NSString *)lb_digestString;
{
    return [LBSHA1HashFromBytes(self.bytes, self.length) base64EncodedStringWithOptions:0];
}
@end

@implementation NSString (LBDigest)
- (NSString *)lb_digestString;
{
    return [LBSHA1HashFromString(self) base64EncodedStringWithOptions:0];
}
@end
查看更多
Animai°情兽
7楼-- · 2019-01-31 06:28

Here's a concise and highly optimized NSString category:

@implementation NSString (PMUtils)
- (NSString *)sha1Hash
{
    NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding];
    NSData *hash = [data sha1Hash];
    return [hash hexString];
} 
@end

@implementation NSData (PMUtils)
- (NSString *) hexString
{
    NSUInteger bytesCount = self.length;
    if (bytesCount) {
        static char const *kHexChars = "0123456789ABCDEF";
        const unsigned char *dataBuffer = self.bytes;
        char *chars = malloc(sizeof(char) * (bytesCount * 2 + 1));
        char *s = chars;
        for (unsigned i = 0; i < bytesCount; ++i) {
            *s++ = kHexChars[((*dataBuffer & 0xF0) >> 4)];
            *s++ = kHexChars[(*dataBuffer & 0x0F)];
            dataBuffer++;
        }
        *s = '\0';
        NSString *hexString = [NSString stringWithUTF8String:chars];
        free(chars);
        return hexString;
    }
    return @"";
}
- (NSData *)sha1Hash
{
    unsigned char digest[CC_SHA1_DIGEST_LENGTH];
    if (CC_SHA1(self.bytes, (CC_LONG)self.length, digest)) {
        return [NSData dataWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];
    }
    return nil;
}
@end
查看更多
登录 后发表回答