mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-08 03:27:38 +00:00
Converted crypto.js to typescript (#47)
This commit is contained in:
+32
-7
@@ -2,6 +2,12 @@ const nacl = require("tweetnacl");
|
|||||||
nacl.util = require("tweetnacl-util");
|
nacl.util = require("tweetnacl-util");
|
||||||
const aes = require("./aes-256-gcm");
|
const aes = require("./aes-256-gcm");
|
||||||
|
|
||||||
|
type encryptAsymmetricProps = {
|
||||||
|
plaintext: string;
|
||||||
|
publicKey: string;
|
||||||
|
privateKey: string;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return assymmetrically encrypted [plaintext] using [publicKey] where
|
* Return assymmetrically encrypted [plaintext] using [publicKey] where
|
||||||
* [publicKey] likely belongs to the recipient.
|
* [publicKey] likely belongs to the recipient.
|
||||||
@@ -13,7 +19,7 @@ const aes = require("./aes-256-gcm");
|
|||||||
* @returns {String} ciphertext - base64-encoded ciphertext
|
* @returns {String} ciphertext - base64-encoded ciphertext
|
||||||
* @returns {String} nonce - base64-encoded nonce
|
* @returns {String} nonce - base64-encoded nonce
|
||||||
*/
|
*/
|
||||||
const encryptAssymmetric = ({ plaintext, publicKey, privateKey }) => {
|
const encryptAssymmetric = ({ plaintext, publicKey, privateKey }: encryptAsymmetricProps): object => {
|
||||||
const nonce = nacl.randomBytes(24);
|
const nonce = nacl.randomBytes(24);
|
||||||
const ciphertext = nacl.box(
|
const ciphertext = nacl.box(
|
||||||
nacl.util.decodeUTF8(plaintext),
|
nacl.util.decodeUTF8(plaintext),
|
||||||
@@ -28,6 +34,13 @@ const encryptAssymmetric = ({ plaintext, publicKey, privateKey }) => {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type decryptAsymmetricProps = {
|
||||||
|
ciphertext: string;
|
||||||
|
nonce: string;
|
||||||
|
publicKey: string;
|
||||||
|
privateKey: string;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return assymmetrically decrypted [ciphertext] using [privateKey] where
|
* Return assymmetrically decrypted [ciphertext] using [privateKey] where
|
||||||
* [privateKey] likely belongs to the recipient.
|
* [privateKey] likely belongs to the recipient.
|
||||||
@@ -38,7 +51,7 @@ const encryptAssymmetric = ({ plaintext, publicKey, privateKey }) => {
|
|||||||
* @param {String} obj.privateKey - base64-encoded private key of the receiver (current user)
|
* @param {String} obj.privateKey - base64-encoded private key of the receiver (current user)
|
||||||
* @param {String} plaintext - UTF8 plaintext
|
* @param {String} plaintext - UTF8 plaintext
|
||||||
*/
|
*/
|
||||||
const decryptAssymmetric = ({ ciphertext, nonce, publicKey, privateKey }) => {
|
const decryptAssymmetric = ({ ciphertext, nonce, publicKey, privateKey }: decryptAsymmetricProps): string => {
|
||||||
const plaintext = nacl.box.open(
|
const plaintext = nacl.box.open(
|
||||||
nacl.util.decodeBase64(ciphertext),
|
nacl.util.decodeBase64(ciphertext),
|
||||||
nacl.util.decodeBase64(nonce),
|
nacl.util.decodeBase64(nonce),
|
||||||
@@ -49,13 +62,18 @@ const decryptAssymmetric = ({ ciphertext, nonce, publicKey, privateKey }) => {
|
|||||||
return nacl.util.encodeUTF8(plaintext);
|
return nacl.util.encodeUTF8(plaintext);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type encryptSymmetricProps = {
|
||||||
|
plaintext: string;
|
||||||
|
key: string;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return symmetrically encrypted [plaintext] using [key].
|
* Return symmetrically encrypted [plaintext] using [key].
|
||||||
* @param {Object} obj
|
* @param {Object} obj
|
||||||
* @param {String} obj.plaintext - plaintext to encrypt
|
* @param {String} obj.plaintext - plaintext to encrypt
|
||||||
* @param {String} obj.key - 16-byte hex key
|
* @param {String} obj.key - 16-byte hex key
|
||||||
*/
|
*/
|
||||||
const encryptSymmetric = ({ plaintext, key }) => {
|
const encryptSymmetric = ({ plaintext, key }: encryptSymmetricProps): object => {
|
||||||
let ciphertext, iv, tag;
|
let ciphertext, iv, tag;
|
||||||
try {
|
try {
|
||||||
const obj = aes.encrypt(plaintext, key);
|
const obj = aes.encrypt(plaintext, key);
|
||||||
@@ -75,6 +93,13 @@ const encryptSymmetric = ({ plaintext, key }) => {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type decryptSymmetricProps = {
|
||||||
|
ciphertext: string;
|
||||||
|
iv: string;
|
||||||
|
tag: string;
|
||||||
|
key: string;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return symmetrically decypted [ciphertext] using [iv], [tag],
|
* Return symmetrically decypted [ciphertext] using [iv], [tag],
|
||||||
* and [key].
|
* and [key].
|
||||||
@@ -85,7 +110,7 @@ const encryptSymmetric = ({ plaintext, key }) => {
|
|||||||
* @param {String} obj.key - 32-byte hex key
|
* @param {String} obj.key - 32-byte hex key
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
const decryptSymmetric = ({ ciphertext, iv, tag, key }) => {
|
const decryptSymmetric = ({ ciphertext, iv, tag, key }: decryptSymmetricProps): string => {
|
||||||
let plaintext;
|
let plaintext;
|
||||||
try {
|
try {
|
||||||
plaintext = aes.decrypt(ciphertext, iv, tag, key);
|
plaintext = aes.decrypt(ciphertext, iv, tag, key);
|
||||||
@@ -97,9 +122,9 @@ const decryptSymmetric = ({ ciphertext, iv, tag, key }) => {
|
|||||||
return plaintext;
|
return plaintext;
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = {
|
export {
|
||||||
encryptAssymmetric,
|
|
||||||
decryptAssymmetric,
|
decryptAssymmetric,
|
||||||
encryptSymmetric,
|
|
||||||
decryptSymmetric,
|
decryptSymmetric,
|
||||||
|
encryptAssymmetric,
|
||||||
|
encryptSymmetric,
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user