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
-
Verify a message MAC
Declaration
Swift
public static func verifyMAC(data: Data, key: Data, expectedMAC: Data) throws
-
RFC 5869 key derivation
Declaration
Swift
public static func deriveSecrets(input: Data, salt: Data = Data(count: 32), info: Data, chunks: Int = 2) throws -> [Data]
-
Calculate an ECDH agreement.
Declaration
Swift
public static func calculateAgreement(publicKeyData: Data, privateKeyData: Data) throws -> Data
Parameters
publicKeyData
The curve25519 (typically remote party’s) 32-byte public key data
privateKeyData
The curve25519 (typically your) 32-byte private key data
Return Value
a 32-bit shared secret on success, throwing on failure
-
Generates a Curve25519 keypair.
Declaration
Swift
public static func generateKeyPairFromPrivateKey(privateKeyData: Data) throws -> KeyPair
Parameters
privateKeyData
The curve25519 (typically your) 32-byte private key data
Return Value
the full
KeyPair
-
Generate a Curve25519 signature.
Declaration
Swift
public static func generateSignature(privateKeyData: Data, message: Data) throws -> Data
-
Validate a Curve25519 signature.
Declaration
Swift
public static func verifySignature(signature: Data, publicKeyData: Data, message: Data) throws -> Bool
-
Decrypt attachment data
Declaration
Swift
public static func decryptAttachment(data: Data, keys: Data) throws -> Data
-
Encrypt attachment data
Declaration
Swift
public static func encryptAttachment(data: Data, keys: Data, iv: Data) throws -> Data