Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save zwaldowski/e9f49a2f8256d5912418ca2cd5765246 to your computer and use it in GitHub Desktop.
Save zwaldowski/e9f49a2f8256d5912418ca2cd5765246 to your computer and use it in GitHub Desktop.
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