SignalCommonCrypto

public struct SignalCommonCrypto

SignalCommonCrypto struct adopted from the LibSignalProtocolSwift project (lightly modified)

  • Create a number of random bytes

    Throws

    SignalError.noRandomBytes

    Declaration

    Swift

    public static func random(bytes: Int) throws -> Data

    Parameters

    bytes

    The number of random bytes to create

    Return Value

    An array of bytes length with random numbers

  • Create a HMAC authentication for a given message

    Declaration

    Swift

    public static func hmacSHA256(for message: Data, with salt: Data) -> Data

    Parameters

    message

    The input message to create the HMAC for

    salt

    The salt for the HMAC

    Return Value

    The HMAC

  • Create a SHA512 digest for a given message

    Throws

    SignalError.digestError

    Declaration

    Swift

    public static func sha512(for message: Data) throws -> Data

    Parameters

    message

    The input message to create the digest for

    Return Value

    The digest

  • Encrypt a message with AES

    Throws

    SignalError.encryptionError

    Declaration

    Swift

    public static func encrypt(message: Data, with cipher: SignalEncryptionScheme = .AES_CBCwithPKCS5, key: Data, iv: Data) throws -> Data

    Parameters

    message

    The input message to encrypt

    cipher

    THe encryption scheme to use

    key

    The key for encryption (kCCKeySizeAES256 bytes)

    iv

    The initialization vector (kCCBlockSizeAES128 bytes)

    Return Value

    The encrypted message

  • Decrypt a message with AES

    Throws

    SignalError.decryptionError

    Declaration

    Swift

    public static func decrypt(message: Data, with cipher: SignalEncryptionScheme = .AES_CBCwithPKCS5, key: Data, iv: Data) throws -> Data

    Parameters

    message

    The input message to decrypt

    cipher

    THe encryption scheme to use

    key

    The key for decryption (kCCKeySizeAES256 bytes)

    iv

    The initialization vector (kCCBlockSizeAES128 bytes)

    Return Value

    The decrypted message