Created
May 13, 2016 17:21
-
-
Save zwaldowski/e9f49a2f8256d5912418ca2cd5765246 to your computer and use it in GitHub Desktop.
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
private func loadCFunction<Function>(named name: String, ofType _: Function.Type = Function.self) -> Function { | |
let sym = dlsym(UnsafeMutablePointer<Void>(bitPattern: -2), name) // RTLD_DEFAULT | |
return unsafeBitCast(sym, Function.self) | |
} | |
private let CC_SHA1_DIGEST_LENGTH = 20 | |
private let CC_SHA1: @convention(c) (UnsafePointer<Void>, Int32, UnsafeMutablePointer<UInt8>) -> UnsafeMutablePointer<UInt8> = loadCFunction(named: "CC_SHA1") | |
extension String { | |
public func SHA1Sum() -> [UInt8] { | |
return withCString { ptr -> [UInt8] in | |
var array = [UInt8](count: CC_SHA1_DIGEST_LENGTH, repeatedValue: 0) | |
_ = CC_SHA1(ptr, numericCast(strlen(ptr)), &array) | |
return array | |
} | |
} | |
} | |
"Hello, world!".SHA1Sum() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment