Clean up and weave crypto into service account permissions

This commit is contained in:
Tuan Dang
2023-03-29 18:07:15 +07:00
parent 729aacc154
commit 88c0a46de3
20 changed files with 360 additions and 240 deletions
@@ -26,6 +26,31 @@ type EncryptAsymmetricProps = {
privateKey: string;
};
/**
* Verify that private key [privateKey] is the one that corresponds to
* the public key [publicKey]
* @param {Object}
* @param {String} - base64-encoded Nacl private key
* @param {String} - base64-encoded Nacl public key
*/
const verifyPrivateKey = ({
privateKey,
publicKey
}: {
privateKey: string;
publicKey: string;
}) => {
const derivedPublicKey = nacl.util.encodeBase64(
nacl.box.keyPair.fromSecretKey(
nacl.util.decodeBase64(privateKey)
).publicKey
);
if (derivedPublicKey !== publicKey) {
throw new Error('Failed to verify private key');
}
}
/**
* Derive a key from password [password] and salt [salt] using Argon2id
* @param {Object} obj
@@ -204,5 +229,6 @@ export {
decryptSymmetric,
deriveArgonKey,
encryptAssymmetric,
encryptSymmetric,
generateKeyPair};
encryptSymmetric,
generateKeyPair,
verifyPrivateKey};