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%
782 B · 35 lines swift
Raw Blame History
1import CommonCrypto2import Foundation34public extension Data {5    /// Two octet checksum as defined in RFC-4880. Sum of all octets, mod 655366    func checksum() -> UInt16 {7        let s = withUnsafeBytes { buf in8            buf.lazy.map(UInt32.init).reduce(UInt32(0), +)9        }10        return UInt16(s % 65535)11    }12}1314public extension Data {15    init(hex: String) {16        self.init([UInt8](hex: hex))17    }1819    var bytes: [UInt8] {20        Array(self)21    }2223    func toHexString() -> String {24        bytes.toHexString()25    }2627    func sha256() -> Data {28        var hash = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))29        withUnsafeBytes {30            _ = CC_SHA256($0.baseAddress, CC_LONG(count), &hash)31        }32        return Data(hash)33    }34}35