Created
February 28, 2015 21:23
-
-
Save advantis/f6b40319dcc4b46b45e3 to your computer and use it in GitHub Desktop.
SHA1 digest in Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
func sha1(data: NSData) -> String { | |
let length = Int(CC_SHA1_DIGEST_LENGTH) | |
var digest = [UInt8](count: length, repeatedValue: 0) | |
CC_SHA1(data.bytes, CC_LONG(data.length), &digest) | |
return digest.map { String(format: "%02x", $0) }.reduce("", +) | |
} | |
func sha1(string: String) -> String? { | |
return string.dataUsingEncoding(NSUTF8StringEncoding).map(sha1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment