@lagoware/capacitor-key-manager
v0.1.1
Published
Utilize and store encryption and signing keys
Downloads
71
Maintainers
Readme
@lagoware/capacitor-key-manager
Utilize and store encryption and signing keys.
- [x] Android
- [x] Web
- [ ] iOS (PRs welcome)
Key Storage
Android
Keys are stored in the Android Key Store.
Web
Keys are stored as non-extractable Web Crypto keys in an IndexedDB database. Please note that your keys may be at risk of eviction by the browser unless your app successfully requests persistent storage.
Install
npm install @lagoware/capacitor-key-manager
npx cap sync
API
checkAliasExists(...)
generateKey(...)
generateRecoverableSignatureKeyPair(...)
generateRecoverableAgreementKeyPair(...)
generateRecoverableKey(...)
importPublicSignatureKey(...)
importPublicAgreementKey(...)
reWrapSignatureKeyPair(...)
reWrapAgreementKeyPair(...)
reWrapKey(...)
recoverKey(...)
recoverSignatureKeyPair(...)
recoverAgreementKeyPair(...)
encrypt(...)
decrypt(...)
encryptWithAgreedKey(...)
decryptWithAgreedKey(...)
sign(...)
verify(...)
- Type Aliases
checkAliasExists(...)
checkAliasExists(options: { keyAlias: string; }) => Promise<{ aliasExists: boolean; }>
Checks if a key or key pair exists in the key store under the provided alias.
| Param | Type |
| ------------- | ---------------------------------- |
| options
| { keyAlias: string; } |
Returns: Promise<{ aliasExists: boolean; }>
generateKey(...)
generateKey(options: { keyAlias: string; }) => Promise<void>
Generates a key that can be used for symmetric encryption / decryption. The underlying key material cannot be recovered, therefore encryption / decryption will only be possible on this device.
| Param | Type |
| ------------- | ---------------------------------- |
| options
| { keyAlias: string; } |
generateRecoverableSignatureKeyPair(...)
generateRecoverableSignatureKeyPair(options: { password: string; salt?: string; }) => Promise<{ recoverableKeyPair: RecoverableKeyPair; }>
Generates a key pair that can be used for signing and verifying strings. The private key will be encrypted with the provided password and if provided, (base64-encoded) salt (otherwise a random salt will be generated). The generated key pair is returned in a structure of base64-encoded strings that may later be used to recover the key into the key store.
| Param | Type |
| ------------- | ------------------------------------------------- |
| options
| { password: string; salt?: string; } |
Returns: Promise<{ recoverableKeyPair: RecoverableKeyPair; }>
generateRecoverableAgreementKeyPair(...)
generateRecoverableAgreementKeyPair(options: { password: string; salt?: string; }) => Promise<{ recoverableKeyPair: RecoverableKeyPair; }>
Generates a key pair that can be used for deriving key agreement secrets. The private key will be encrypted with the provided password and if provided, (base64-encoded) salt (otherwise a random salt will be generated). The generated key pair is returned in a structure of base64-encoded strings that may later be used to recover the key into the key store.
| Param | Type |
| ------------- | ------------------------------------------------- |
| options
| { password: string; salt?: string; } |
Returns: Promise<{ recoverableKeyPair: RecoverableKeyPair; }>
generateRecoverableKey(...)
generateRecoverableKey(options: { password: string; salt?: string; }) => Promise<{ recoverableKey: RecoverableKey; }>
Generates a key that can be used for symmetrical encryption / decryption. The key will be encrypted with the provided password and if provided, (base64-encoded) salt (otherwise a random salt will be generated). The generated key is returned as a structure of base64-encoded strings that may later be used to recover the key into the key store.
| Param | Type |
| ------------- | ------------------------------------------------- |
| options
| { password: string; salt?: string; } |
Returns: Promise<{ recoverableKey: RecoverableKey; }>
importPublicSignatureKey(...)
importPublicSignatureKey(options: { alias: string; publicKey: string; }) => Promise<void>
Imports a public key into the key store. The key may then be used for verifying signatures. The key is expected to be in base64-encoded spki format.
| Param | Type |
| ------------- | -------------------------------------------------- |
| options
| { alias: string; publicKey: string; } |
importPublicAgreementKey(...)
importPublicAgreementKey(options: { alias: string; publicKey: string; }) => Promise<void>
Imports a public key into the key store. The key may then be used to derive key agreement secrets. The key is expected to be in base64-encoded spki format.
| Param | Type |
| ------------- | -------------------------------------------------- |
| options
| { alias: string; publicKey: string; } |
reWrapSignatureKeyPair(...)
reWrapSignatureKeyPair(options: { currentPassword: string; newPassword: string; newSalt?: string; recoverableKeyPair: RecoverableKeyPair; }) => Promise<{ recoverableKeyPair: RecoverableKeyPair; }>
Re-wraps a recoverable signature key pair with a new password.
| Param | Type |
| ------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
| options
| { currentPassword: string; newPassword: string; newSalt?: string; recoverableKeyPair: RecoverableKeyPair; } |
Returns: Promise<{ recoverableKeyPair: RecoverableKeyPair; }>
reWrapAgreementKeyPair(...)
reWrapAgreementKeyPair(options: { currentPassword: string; newPassword: string; newSalt?: string; recoverableKeyPair: RecoverableKeyPair; }) => Promise<{ recoverableKeyPair: RecoverableKeyPair; }>
Re-wraps a recoverable agreement key pair with a new password.
| Param | Type |
| ------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
| options
| { currentPassword: string; newPassword: string; newSalt?: string; recoverableKeyPair: RecoverableKeyPair; } |
Returns: Promise<{ recoverableKeyPair: RecoverableKeyPair; }>
reWrapKey(...)
reWrapKey(options: { currentPassword: string; newPassword: string; newSalt?: string; recoverableKey: RecoverableKey; }) => Promise<{ recoverableKey: RecoverableKey; }>
Re-wraps a recoverable key with a new password.
| Param | Type |
| ------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
| options
| { currentPassword: string; newPassword: string; newSalt?: string; recoverableKey: RecoverableKey; } |
Returns: Promise<{ recoverableKey: RecoverableKey; }>
recoverKey(...)
recoverKey(options: { alias: string; recoverableKey: RecoverableKey; password: string; }) => Promise<void>
Imports a previously-generated recoverable key into the key store. If the provided password matches the password used when the key was generated, it will be decrypted and saved into the key store. It may then be used to encrypt and decrypt values.
| Param | Type |
| ------------- | --------------------------------------------------------------------------------------------------------------- |
| options
| { alias: string; recoverableKey: RecoverableKey; password: string; } |
recoverSignatureKeyPair(...)
recoverSignatureKeyPair(options: { alias: string; recoverableKeyPair: RecoverableKeyPair; password: string; }) => Promise<void>
Imports a previously-generated recoverable signature key pair into the key store. If the provided password matches the password used when the key pair was generated, it will be decrypted and saved into the key store. It may then be used to sign and verify signatures.
| Param | Type |
| ------------- | --------------------------------------------------------------------------------------------------------------------------- |
| options
| { alias: string; recoverableKeyPair: RecoverableKeyPair; password: string; } |
recoverAgreementKeyPair(...)
recoverAgreementKeyPair(options: { alias: string; recoverableKeyPair: RecoverableKeyPair; password: string; }) => Promise<void>
Imports a previously-generated recoverable agreement key pair into the key store. If the provided password matches the password used when the key pair was generated, it will be decrypted and saved into the key store. It may then be used to derive key agreement secrets.
| Param | Type |
| ------------- | --------------------------------------------------------------------------------------------------------------------------- |
| options
| { alias: string; recoverableKeyPair: RecoverableKeyPair; password: string; } |
encrypt(...)
encrypt(options: { keyAlias: string; cleartext: string; }) => Promise<{ encryptedMessage: EncryptedMessage; }>
Encrypts a string with a previously generated / recovered key. The encrypted string is returned in a structure of base64-encoded strings.
| Param | Type |
| ------------- | ----------------------------------------------------- |
| options
| { keyAlias: string; cleartext: string; } |
Returns: Promise<{ encryptedMessage: EncryptedMessage; }>
decrypt(...)
decrypt(options: { keyAlias: string; encryptedMessage: EncryptedMessage; }) => Promise<{ cleartext: string; }>
Decrypts a string with a previously generated / recovered key.
| Param | Type |
| ------------- | ------------------------------------------------------------------------------------------------------ |
| options
| { keyAlias: string; encryptedMessage: EncryptedMessage; } |
Returns: Promise<{ cleartext: string; }>
encryptWithAgreedKey(...)
encryptWithAgreedKey(options: { privateKeyAlias: string; publicKeyAlias: string; cleartext: string; info?: string; }) => Promise<{ encryptedMessage: EncryptedMessage; }>
Encrypts a string with a key derived from the provided private and public agreement keys. If info parameter is provided, it will be used to further derive the key before encryption.
| Param | Type |
| ------------- | --------------------------------------------------------------------------------------------------- |
| options
| { privateKeyAlias: string; publicKeyAlias: string; cleartext: string; info?: string; } |
Returns: Promise<{ encryptedMessage: EncryptedMessage; }>
decryptWithAgreedKey(...)
decryptWithAgreedKey(options: { privateKeyAlias: string; publicKeyAlias: string; encryptedMessage: EncryptedMessage; info?: string; }) => Promise<{ cleartext: string; }>
Decrypts a string with a key derived from the provided private and public agreement keys. If info parameter is provided, it will be used to further derive the key before decryption.
| Param | Type |
| ------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- |
| options
| { privateKeyAlias: string; publicKeyAlias: string; encryptedMessage: EncryptedMessage; info?: string; } |
Returns: Promise<{ cleartext: string; }>
sign(...)
sign(options: { keyAlias: string; cleartext: string; }) => Promise<{ signature: string; }>
Signs a string with a previously generated / recovered signature key pair.
| Param | Type |
| ------------- | ----------------------------------------------------- |
| options
| { keyAlias: string; cleartext: string; } |
Returns: Promise<{ signature: string; }>
verify(...)
verify(options: { keyAlias: string; cleartext: string; signature: string; }) => Promise<{ isValid: boolean; }>
Verifies a signature with a previously generated / recovered signature key pair or imported public key.
| Param | Type |
| ------------- | ------------------------------------------------------------------------ |
| options
| { keyAlias: string; cleartext: string; signature: string; } |
Returns: Promise<{ isValid: boolean; }>
Type Aliases
RecoverableKeyPair
{ privateKey: RecoverableKey, publicKey: string }
RecoverableKey
EncryptedMessage & { salt: string }
EncryptedMessage
{ ciphertext: string, iv: string }