Zoul solution ported to Swift 3 :
import Foundation func SHA1HashString(string: String) -> String { let task = Process() task.launchPath = "/usr/bin/shasum" task.arguments = [] let inputPipe = Pipe() inputPipe.fileHandleForWriting.write(string.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: "") }
Note. Works on a terminal with Apple Swift version 3.0.2 (swiftlang-800.0.63 clang-800.0.42.1) , but see, it works on Ubuntu: Error: using an unauthorized authentication process .
source share