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%
2.1 KB · 65 lines swift
Raw Blame History
1import WalletCoreC2import Foundation3// SPDX-License-Identifier: Apache-2.04//5// Copyright © 2017 Trust Wallet.6//7// This is a GENERATED FILE, changes made here WILL BE LOST.8//910import Foundation1112/// Password-Based Key Derivation Function 213public struct PBKDF2 {1415    /// Derives a key from a password and a salt using PBKDF2 + Sha256.16    ///17    /// - Parameter password: is the master password from which a derived key is generated18    /// - Parameter salt: is a sequence of bits, known as a cryptographic salt19    /// - Parameter iterations: is the number of iterations desired20    /// - Parameter dkLen: is the desired bit-length of the derived key21    /// - Returns: the derived key data.22    public static func hmacSha256(password: Data, salt: Data, iterations: UInt32, dkLen: UInt32) -> Data? {23        let passwordData = TWDataCreateWithNSData(password)24        defer {25            TWDataDelete(passwordData)26        }27        let saltData = TWDataCreateWithNSData(salt)28        defer {29            TWDataDelete(saltData)30        }31        guard let result = TWPBKDF2HmacSha256(passwordData, saltData, iterations, dkLen) else {32            return nil33        }34        return TWDataNSData(result)35    }3637    /// Derives a key from a password and a salt using PBKDF2 + Sha512.38    ///39    /// - Parameter password: is the master password from which a derived key is generated40    /// - Parameter salt: is a sequence of bits, known as a cryptographic salt41    /// - Parameter iterations: is the number of iterations desired42    /// - Parameter dkLen: is the desired bit-length of the derived key43    /// - Returns: the derived key data.44    public static func hmacSha512(password: Data, salt: Data, iterations: UInt32, dkLen: UInt32) -> Data? {45        let passwordData = TWDataCreateWithNSData(password)46        defer {47            TWDataDelete(passwordData)48        }49        let saltData = TWDataCreateWithNSData(salt)50        defer {51            TWDataDelete(saltData)52        }53        guard let result = TWPBKDF2HmacSha512(passwordData, saltData, iterations, dkLen) else {54            return nil55        }56        return TWDataNSData(result)57    }585960    init() {61    }626364}65