-
-
Save own2pwn/934cd90632b82d5c72cc47e239abe5f7 to your computer and use it in GitHub Desktop.
SHA1 digest in Swift
This file contains 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