Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Rici's Coding Soup - SHA-1 Function (Article: Blockchain in Swift)
extension String {
/// Generates SHA1 Hashes
func toSHA1() -> String {
let task = Process()
task.launchPath = "/usr/bin/shasum"
task.arguments = []
let inputPipe = Pipe()
inputPipe.fileHandleForWriting.write(self.data(using: String.Encoding.utf8)!)
inputPipe.fileHandleForWriting.closeFile()
let outputPipe = Pipe()
task.standardOutput = outputPipe
task.standardInput = inputPipe
task.launch()
let data = outputPipe.fileHandleForReading.readDataToEndOfFile()
let hash = String(data: data, encoding: String.Encoding.utf8)!
return hash.replacingOccurrences(of: " -\n", with: "")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment