Rici's Coding Soup - SHA-1 Function (Article: Blockchain in Swift)
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
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