有没有办法在 Swift 操场上使用 Common Crypto?
Is there a way to use Common Crypto in a Swift playground?
我正在 Xcode 游乐场玩 REST API,我需要用 SHA1 散列一些东西。我找到的所有解决方案都依赖于 Common Crypto,而且似乎无法直接在 Swift 游乐场中使用。有没有办法在 Swift 操场上进行 SHA1 运算?
一个快速而肮脏的解决方案:
func SHA1HashString(string: String) -> String {
let task = NSTask()
task.launchPath = "/usr/bin/shasum"
task.arguments = []
let inputPipe = NSPipe()
inputPipe.fileHandleForWriting.writeData(string.dataUsingEncoding(NSUTF8StringEncoding)!)
inputPipe.fileHandleForWriting.closeFile()
let outputPipe = NSPipe()
task.standardOutput = outputPipe
task.standardInput = inputPipe
task.launch()
let data = outputPipe.fileHandleForReading.readDataToEndOfFile()
let hash = String(data: data, encoding: NSUTF8StringEncoding)!
return hash.stringByReplacingOccurrencesOfString(" -\n", withString: "")
}
Zoul 的解决方案,移植到 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: "")
}
注意:使用 Apple Swift version 3.0.2 (swiftlang-800.0.63 clang-800.0.42.1)
在终端上工作,但是请参阅 Ubuntu 上的 运行:
有一个 CommonCrypto Swift 包装器 https://github.com/onmyway133/Arcane
您应该能够将它导入您的 playground,因为在存储库中有 MacOS 和 iOS playground 使用它的示例。
我正在 Xcode 游乐场玩 REST API,我需要用 SHA1 散列一些东西。我找到的所有解决方案都依赖于 Common Crypto,而且似乎无法直接在 Swift 游乐场中使用。有没有办法在 Swift 操场上进行 SHA1 运算?
一个快速而肮脏的解决方案:
func SHA1HashString(string: String) -> String {
let task = NSTask()
task.launchPath = "/usr/bin/shasum"
task.arguments = []
let inputPipe = NSPipe()
inputPipe.fileHandleForWriting.writeData(string.dataUsingEncoding(NSUTF8StringEncoding)!)
inputPipe.fileHandleForWriting.closeFile()
let outputPipe = NSPipe()
task.standardOutput = outputPipe
task.standardInput = inputPipe
task.launch()
let data = outputPipe.fileHandleForReading.readDataToEndOfFile()
let hash = String(data: data, encoding: NSUTF8StringEncoding)!
return hash.stringByReplacingOccurrencesOfString(" -\n", withString: "")
}
Zoul 的解决方案,移植到 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: "")
}
注意:使用 Apple Swift version 3.0.2 (swiftlang-800.0.63 clang-800.0.42.1)
在终端上工作,但是请参阅 Ubuntu 上的 运行:
有一个 CommonCrypto Swift 包装器 https://github.com/onmyway133/Arcane
您应该能够将它导入您的 playground,因为在存储库中有 MacOS 和 iOS playground 使用它的示例。