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.
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.
try this:
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:
Once imported there, you do not need anything else. Just use String extension provided:
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 :)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.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
I'm seeing a few different possible improvements to the answers in this post.
Here's my solution, adapted from the excellent SocketRocket library's SRHash.m:
Here's a concise and highly optimized NSString category: