swift-playground - 有没有办法在 Swift playground 中使用 Common Crypto?

标签 swift-playground commoncrypto

我正在 Xcode Playground 上玩弄 REST API,我需要用 SHA1 散列一些东西。我找到的所有解决方案都依赖于 Common Crypto,而这似乎不能直接在 Swift playground 中使用。有没有办法在 Swift playground 中对某些东西进行 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: "")
}

关于swift-playground - 有没有办法在 Swift playground 中使用 Common Crypto?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33032758/

相关文章:

ios - 如何在 Swift Playground 中获取布局跟踪

arrays - Swift3 - 扩展类型 Collection 的问题

ios - AES 中的 CCCrypt 解密

快速解析 XML

swift - 如何将 4 个字节转换为 Swift float ?

ios - 通用加密——测试解密中的坏 key ?

ios - 在 Swift 中使用 CCCrypt (CommonCrypt) 的问题

ios - 使用RNCryptor的iPhone App是否需要任何加密注册?

CryptoApi 到 CommonCrypto

swift - 如何在 swift playground 中使用 carthage 导入的框架