AndyIbanez / andyibanez-com

Static website.
1 stars 0 forks source link

posts/cryptokit-not-enough/ #9

Open utterances-bot opened 4 years ago

utterances-bot commented 4 years ago

When CryptoKit is not Enough • Andy Ibanez

https://www.andyibanez.com/posts/cryptokit-not-enough/

muhasturk commented 4 years ago

Amazing article thank you.

Fix following method usage of digest.

func dataToHexString(_ data: Data) -> String {
  let hexBytes = data.map { String(format: "%02hhx", $0) }
  return hexBytes.joined()
}

By the way this data extension is faster for converting data to hexEncodedString.

public extension Data {
    enum HexEncodingOption {
        case upperCase
    }

    func hexEncodedString(options: [HexEncodingOption] = []) -> String {
        let hexDigits = Array((options.contains(.upperCase) ? "0123456789ABCDEF" : "0123456789abcdef").utf16)
        var chars: [unichar] = []
        chars.reserveCapacity(2 * count)
        for byte in self {
            chars.append(hexDigits[Int(byte / 16)])
            chars.append(hexDigits[Int(byte % 16)])
        }
        return String(utf16CodeUnits: chars, count: chars.count)
    }
snej commented 3 years ago

Using OpenSSL is kind of a last resort — it's a huge library, and as you point out the API is complex. I avoid it whenever possible. 😬

libSodium is a good alternative. It's much smaller and more modern, and like CryptoKit its API is designed to make it hard to shoot yourself in the foot. It doesn't have as many algorithms, but it does offer Ed25519, Salsa20, ChaCha20, Poly1305, Argon2 and Scrypt.