CryptoHandler

class CryptoHandler

Handles all encryption of messages as well as public and private keys.

  • Gets your public key for generation of the QR code.

    Gets the saved public key from UserDefaults if there is one. Otherwise it will generate a new public key.

    Declaration

    Swift

    static func getPublicKey() -> String

    Return Value

    Your public key as a string.

  • Returns your private key which is saved as a string in UserDefaults.

    Declaration

    Swift

    static func getPrivateKey() -> P256.KeyAgreement.PrivateKey

    Return Value

    Your private key as a private key object.

  • Generate a new private key for you.

    This is only used when a username has been set. This private key also holds your public key. It can be accessed with privatekey.publickey

    Declaration

    Swift

    static func generatePrivateKey() -> P256.KeyAgreement.PrivateKey

    Return Value

    Your new private key.

  • Converts a private key object into a string since objects cannot be saved to storage.

    Declaration

    Swift

    static func exportPrivateKey(_ privateKey: P256.KeyAgreement.PrivateKey) -> String

    Parameters

    privateKey

    The private key to convert.

    Return Value

    The private key as a string.

  • Converts a public key object into a string since objects cannot be saved to storage.

    Declaration

    Swift

    static func exportPublicKey(_ publicKey: P256.KeyAgreement.PublicKey) -> String

    Parameters

    publicKey

    The public key to convert.

    Return Value

    The public key as a string.

  • Convert a private key string into a private key object.

    Throws

    Throws if the private key string cannot be converted to an object due to wrong formatting.

    Declaration

    Swift

    static func importPrivateKey(_ privateKey: String) throws -> P256.KeyAgreement.PrivateKey

    Parameters

    privateKey

    The private key as a string to convert.

    Return Value

    The private key as an object.

  • Convert a public key string into a public key object.

    Throws

    Throws if the public key string cannot be converted to an object due to wrong formatting.

    Declaration

    Swift

    static func importPublicKey(_ publicKey: String) throws -> P256.KeyAgreement.PublicKey

    Parameters

    publicKey

    The public key as a string to convert.

    Return Value

    The public key as an object.

  • Derive the symmetric key to use for encryption.

    Throws

    If the shared secret cannot be derived. Most likely due to wrong formatting.

    Declaration

    Swift

    static func deriveSymmetricKey(privateKey: P256.KeyAgreement.PrivateKey, publicKey: P256.KeyAgreement.PublicKey) throws -> SymmetricKey

    Parameters

    privateKey

    The private key to use (yours)

    publicKey

    The public key to use (the receiver of the message)

    Return Value

    The symmetric key to use for encryption.

  • Encrypt a string given a symmetric key.

    Throws

    If the encryption failed for some reason.

    Declaration

    Swift

    static func encryptMessage(text: String, symmetricKey: SymmetricKey) throws -> String

    Parameters

    text

    The string to encrypt.

    symmetricKey

    The symmetric key to use.

    Return Value

    An encrypted string which is unreadable.

  • Decrypt a message that you have received.

    Declaration

    Swift

    static func decryptMessage(text: String, symmetricKey: SymmetricKey) -> String

    Parameters

    text

    The string to decrypt.

    symmetricKey

    The symmetric key to use.

    Return Value

    A human-readable string.