SPB Git

spb/os-vault Public

Self-custody, multi-chain crypto wallet for macOS. One recovery phrase, six chain families, zero API keys — nothing leaves your Mac.

Swift 96% Shell 3.4% Makefile 0.6%
1.6 KB · 55 lines swift
Raw Blame History
1import CommonCrypto2import Foundation34func hmac(hmacAlgorithm: HMACAlgorithm, message: Data, key: Data) -> Data? {5    let hashAlgorithm = hmacAlgorithm.HMACAlgorithm6    let length = hmacAlgorithm.digestLength7    var macData = Data(count: Int(length))89    macData.withUnsafeMutableBytes { macBytes in10        message.withUnsafeBytes { messageBytes in11            key.withUnsafeBytes { keyBytes in12                CCHmac(CCHmacAlgorithm(hashAlgorithm),13                       keyBytes,14                       key.count,15                       messageBytes,16                       message.count,17                       macBytes)18            }19        }20    }21    return macData22}2324func hmacSha512(message: Data, key: Data) -> Data? {25    let messageData = message26    let keyData = key27    return hmac(hmacAlgorithm: .SHA512, message: messageData, key: keyData)28}2930enum HMACAlgorithm {31    case MD5, SHA1, SHA224, SHA256, SHA384, SHA5123233    var HMACAlgorithm: Int {34        switch self {35        case .MD5: return kCCHmacAlgMD536        case .SHA1: return kCCHmacAlgSHA137        case .SHA224: return kCCHmacAlgSHA22438        case .SHA256: return kCCHmacAlgSHA25639        case .SHA384: return kCCHmacAlgSHA38440        case .SHA512: return kCCHmacAlgSHA51241        }42    }4344    var digestLength: Int32 {45        switch self {46        case .MD5: return CC_MD5_DIGEST_LENGTH47        case .SHA1: return CC_SHA1_DIGEST_LENGTH48        case .SHA224: return CC_SHA224_DIGEST_LENGTH49        case .SHA256: return CC_SHA256_DIGEST_LENGTH50        case .SHA384: return CC_SHA384_DIGEST_LENGTH51        case .SHA512: return CC_SHA512_DIGEST_LENGTH52        }53    }54}55