Skip to content

Instantly share code, notes, and snippets.

Last active November 3, 2021 09:13
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( String.Encoding.utf8)!)
let outputPipe = Pipe()
task.standardOutput = outputPipe
task.standardInput = inputPipe
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