Skip to content

Instantly share code, notes, and snippets.

@felipericieri
Last active November 3, 2021 09:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felipericieri/c296fa28198b8671a927ec550a522e53 to your computer and use it in GitHub Desktop.
Save felipericieri/c296fa28198b8671a927ec550a522e53 to your computer and use it in GitHub Desktop.
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