@am92/jose-crypto
Advanced tools
Comparing version 2.0.2 to 2.0.3
@@ -0,1 +1,4 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.SERVICE = void 0; | ||
/** @ignore */ | ||
@@ -5,2 +8,2 @@ const { npm_package_name: pkgName = '', npm_package_version: pkgVersion = '' } = process.env; | ||
const SERVICE = `${pkgName}@${pkgVersion}`; | ||
export { SERVICE }; | ||
exports.SERVICE = SERVICE; |
@@ -0,4 +1,7 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.DECRYPT_DATA_INVALID_AES_KEY_LENGTH_ERROR = exports.DECRYPT_DATA_INVALID_AES_KEY_ERROR = exports.DECRYPT_DATA_AES_KEY_REQUIRED_ERROR = exports.DECRYPT_DATA_INVALID_AUTHTAG_LENGTH_ERROR = exports.DECRYPT_DATA_INVALID_IV_LENGTH_ERROR = exports.DECRYPT_DATA_INVALID_PAYLOAD_ERROR = exports.DECRYPT_DATA_PAYLOAD_REQUIRED_ERROR = exports.ENCRYPT_DATA_INVALID_AES_KEY_LENGTH_ERROR = exports.ENCRYPT_DATA_INVALID_AES_KEY_ERROR = exports.ENCRYPT_DATA_AES_KEY_REQUIRED_ERROR = exports.DECRYPT_KEY_INVALID_RSA_KEY_ERROR = exports.DECRYPT_KEY_INVALID_AES_KEY_ERROR = exports.DECRYPT_KEY_RSA_KEY_REQUIRED_ERROR = exports.DECRYPT_KEY_AES_KEY_REQUIRED_ERROR = exports.ENCRYPT_KEY_INVALID_AES_KEY_LENGTH_ERROR = exports.ENCRYPT_KEY_INVALID_RSA_KEY_ERROR = exports.ENCRYPT_KEY_INVALID_AES_KEY_ERROR = exports.ENCRYPT_KEY_RSA_KEY_REQUIRED_ERROR = exports.ENCRYPT_KEY_AES_KEY_REQUIRED_ERROR = void 0; | ||
/** =============== Encrypt Key Errors =============== */ | ||
/** @ignore */ | ||
export const ENCRYPT_KEY_AES_KEY_REQUIRED_ERROR = { | ||
exports.ENCRYPT_KEY_AES_KEY_REQUIRED_ERROR = { | ||
message: 'aesKey is required', | ||
@@ -8,3 +11,3 @@ errorCode: 'JoseCrypto::ENCRYPT_KEY_AES_KEY_REQUIRED' | ||
/** @ignore */ | ||
export const ENCRYPT_KEY_RSA_KEY_REQUIRED_ERROR = { | ||
exports.ENCRYPT_KEY_RSA_KEY_REQUIRED_ERROR = { | ||
message: 'rsaPublicKey is required', | ||
@@ -14,3 +17,3 @@ errorCode: 'JoseCrypto::ENCRYPT_KEY_RSA_KEY_REQUIRED' | ||
/** @ignore */ | ||
export const ENCRYPT_KEY_INVALID_AES_KEY_ERROR = { | ||
exports.ENCRYPT_KEY_INVALID_AES_KEY_ERROR = { | ||
message: 'Invalid aesKey type. Must be a string', | ||
@@ -20,3 +23,3 @@ errorCode: 'JoseCrypto::ENCRYPT_KEY_INVALID_AES_KEY' | ||
/** @ignore */ | ||
export const ENCRYPT_KEY_INVALID_RSA_KEY_ERROR = { | ||
exports.ENCRYPT_KEY_INVALID_RSA_KEY_ERROR = { | ||
message: 'Invalid rsaPublicKey type. Must be a string', | ||
@@ -26,3 +29,3 @@ errorCode: 'JoseCrypto::ENCRYPT_KEY_INVALID_RSA_KEY' | ||
/** @ignore */ | ||
export const ENCRYPT_KEY_INVALID_AES_KEY_LENGTH_ERROR = { | ||
exports.ENCRYPT_KEY_INVALID_AES_KEY_LENGTH_ERROR = { | ||
message: 'Invalid aesKey length', | ||
@@ -34,3 +37,3 @@ errorCode: 'JoseCrypto::ENCRYPT_KEY_INVALID_AES_KEY_LENGTH' | ||
/** @ignore */ | ||
export const DECRYPT_KEY_AES_KEY_REQUIRED_ERROR = { | ||
exports.DECRYPT_KEY_AES_KEY_REQUIRED_ERROR = { | ||
message: 'encryptedAesKey is required', | ||
@@ -40,3 +43,3 @@ errorCode: 'JoseCrypto::DECRYPT_KEY_AES_KEY_REQUIRED' | ||
/** @ignore */ | ||
export const DECRYPT_KEY_RSA_KEY_REQUIRED_ERROR = { | ||
exports.DECRYPT_KEY_RSA_KEY_REQUIRED_ERROR = { | ||
message: 'rsaPrivateKey is required', | ||
@@ -46,3 +49,3 @@ errorCode: 'JoseCrypto::DECRYPT_KEY_RSA_KEY_REQUIRED' | ||
/** @ignore */ | ||
export const DECRYPT_KEY_INVALID_AES_KEY_ERROR = { | ||
exports.DECRYPT_KEY_INVALID_AES_KEY_ERROR = { | ||
message: 'Invalid encryptedAesKey type. Must be a string', | ||
@@ -52,3 +55,3 @@ errorCode: 'JoseCrypto::DECRYPT_KEY_INVALID_AES_KEY' | ||
/** @ignore */ | ||
export const DECRYPT_KEY_INVALID_RSA_KEY_ERROR = { | ||
exports.DECRYPT_KEY_INVALID_RSA_KEY_ERROR = { | ||
message: 'Invalid rsaPrivateKey type. Must be a string', | ||
@@ -60,3 +63,3 @@ errorCode: 'JoseCrypto::DECRYPT_KEY_INVALID_RSA_KEY' | ||
/** @ignore */ | ||
export const ENCRYPT_DATA_AES_KEY_REQUIRED_ERROR = { | ||
exports.ENCRYPT_DATA_AES_KEY_REQUIRED_ERROR = { | ||
message: 'aesKey is required', | ||
@@ -66,3 +69,3 @@ errorCode: 'JoseCrypto::ENCRYPT_DATA_AES_KEY_REQUIRED' | ||
/** @ignore */ | ||
export const ENCRYPT_DATA_INVALID_AES_KEY_ERROR = { | ||
exports.ENCRYPT_DATA_INVALID_AES_KEY_ERROR = { | ||
message: 'Invalid aesKey type. Must be a string', | ||
@@ -72,3 +75,3 @@ errorCode: 'JoseCrypto::ENCRYPT_DATA_INVALID_AES_KEY' | ||
/** @ignore */ | ||
export const ENCRYPT_DATA_INVALID_AES_KEY_LENGTH_ERROR = { | ||
exports.ENCRYPT_DATA_INVALID_AES_KEY_LENGTH_ERROR = { | ||
message: 'Invalid aesKey length', | ||
@@ -80,3 +83,3 @@ errorCode: 'JoseCrypto::ENCRYPT_DATA_INVALID_AES_KEY_LENGTH' | ||
/** @ignore */ | ||
export const DECRYPT_DATA_PAYLOAD_REQUIRED_ERROR = { | ||
exports.DECRYPT_DATA_PAYLOAD_REQUIRED_ERROR = { | ||
message: 'payload is required', | ||
@@ -86,3 +89,3 @@ errorCode: 'JoseCrypto::DECRYPT_DATA_PAYLOAD_REQUIRED' | ||
/** @ignore */ | ||
export const DECRYPT_DATA_INVALID_PAYLOAD_ERROR = { | ||
exports.DECRYPT_DATA_INVALID_PAYLOAD_ERROR = { | ||
message: 'Invalid payload. Must be a string of format {IV.AUTH_TAG.CIPHERTEXT}', | ||
@@ -92,3 +95,3 @@ errorCode: 'JoseCrypto::DECRYPT_DATA_INVALID_PAYLOAD' | ||
/** @ignore */ | ||
export const DECRYPT_DATA_INVALID_IV_LENGTH_ERROR = { | ||
exports.DECRYPT_DATA_INVALID_IV_LENGTH_ERROR = { | ||
message: 'Invalid IV length in payload', | ||
@@ -98,3 +101,3 @@ errorCode: 'JoseCrypto::DECRYPT_DATA_INVALID_IV_LENGTH' | ||
/** @ignore */ | ||
export const DECRYPT_DATA_INVALID_AUTHTAG_LENGTH_ERROR = { | ||
exports.DECRYPT_DATA_INVALID_AUTHTAG_LENGTH_ERROR = { | ||
message: 'Invalid authTag length in payload', | ||
@@ -104,3 +107,3 @@ errorCode: 'JoseCrypto::DECRYPT_DATA_INVALID_AUTHTAG_LENGTH' | ||
/** @ignore */ | ||
export const DECRYPT_DATA_AES_KEY_REQUIRED_ERROR = { | ||
exports.DECRYPT_DATA_AES_KEY_REQUIRED_ERROR = { | ||
message: 'aesKey is required', | ||
@@ -110,3 +113,3 @@ errorCode: 'JoseCrypto::DECRYPT_DATA_AES_KEY_REQUIRED' | ||
/** @ignore */ | ||
export const DECRYPT_DATA_INVALID_AES_KEY_ERROR = { | ||
exports.DECRYPT_DATA_INVALID_AES_KEY_ERROR = { | ||
message: 'Invalid aesKey type. Must be a string', | ||
@@ -116,3 +119,3 @@ errorCode: 'JoseCrypto::DECRYPT_DATA_INVALID_AES_KEY' | ||
/** @ignore */ | ||
export const DECRYPT_DATA_INVALID_AES_KEY_LENGTH_ERROR = { | ||
exports.DECRYPT_DATA_INVALID_AES_KEY_LENGTH_ERROR = { | ||
message: 'Invalid aesKey length', | ||
@@ -119,0 +122,0 @@ errorCode: 'JoseCrypto::DECRYPT_DATA_INVALID_AES_KEY_LENGTH' |
@@ -1,5 +0,25 @@ | ||
import JoseCrypto from './JoseCrypto'; | ||
import JoseCryptoError from './JoseCryptoError'; | ||
export * from './TYPES'; | ||
export { JoseCryptoError }; | ||
export default JoseCrypto; | ||
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.JoseCryptoError = void 0; | ||
const JoseCrypto_1 = __importDefault(require("./JoseCrypto")); | ||
const JoseCryptoError_1 = __importDefault(require("./JoseCryptoError")); | ||
exports.JoseCryptoError = JoseCryptoError_1.default; | ||
__exportStar(require("./TYPES"), exports); | ||
exports.default = JoseCrypto_1.default; |
@@ -1,5 +0,10 @@ | ||
import crypto from 'crypto'; | ||
import JoseCryptoError from './JoseCryptoError'; | ||
import { validateDecryptDataKey, validateDecryptDataPayload, validateDecryptKey, validateEncryptData, validateEncryptKey } from './validators'; | ||
import { derToPem, jsonParse } from './utils'; | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const crypto_1 = __importDefault(require("crypto")); | ||
const JoseCryptoError_1 = __importDefault(require("./JoseCryptoError")); | ||
const validators_1 = require("./validators"); | ||
const utils_1 = require("./utils"); | ||
/** | ||
@@ -11,3 +16,3 @@ * JoseCrypto Class to implement JOSE Cryptography. | ||
*/ | ||
export default class JoseCrypto { | ||
class JoseCrypto { | ||
/** | ||
@@ -32,11 +37,11 @@ * Function to encrypt AES-256-GCM key using `RSA-OAEP` public key. | ||
static encryptKey(aesKey, rsaPublicKey) { | ||
const keyBuffer = validateEncryptKey(aesKey, rsaPublicKey); | ||
const rsaPemKey = derToPem(rsaPublicKey, 'PUBLIC KEY'); | ||
const keyBuffer = (0, validators_1.validateEncryptKey)(aesKey, rsaPublicKey); | ||
const rsaPemKey = (0, utils_1.derToPem)(rsaPublicKey, 'PUBLIC KEY'); | ||
try { | ||
const wrapParams = { | ||
key: rsaPemKey, | ||
padding: crypto.constants.RSA_PKCS1_OAEP_PADDING, | ||
padding: crypto_1.default.constants.RSA_PKCS1_OAEP_PADDING, | ||
oaepHash: 'sha256' | ||
}; | ||
const encryptedKeyBuffer = crypto.publicEncrypt(wrapParams, keyBuffer); | ||
const encryptedKeyBuffer = crypto_1.default.publicEncrypt(wrapParams, keyBuffer); | ||
const encryptedAesKey = encryptedKeyBuffer.toString('base64'); | ||
@@ -47,3 +52,3 @@ return encryptedAesKey; | ||
const errorCode = `JoseCrypto::DECRYPT_KEY_${error.code || 'UNKNOWN_ERR_CODE'}`; | ||
throw new JoseCryptoError(error, { errorCode }); | ||
throw new JoseCryptoError_1.default(error, { errorCode }); | ||
} | ||
@@ -68,11 +73,11 @@ } | ||
static decryptKey(encryptedAesKey, rsaPrivateKey) { | ||
const encryptedKeyBuffer = validateDecryptKey(encryptedAesKey, rsaPrivateKey); | ||
const rsaPemKey = derToPem(rsaPrivateKey, 'PRIVATE KEY'); | ||
const encryptedKeyBuffer = (0, validators_1.validateDecryptKey)(encryptedAesKey, rsaPrivateKey); | ||
const rsaPemKey = (0, utils_1.derToPem)(rsaPrivateKey, 'PRIVATE KEY'); | ||
try { | ||
const unwrapParams = { | ||
key: rsaPemKey, | ||
padding: crypto.constants.RSA_PKCS1_OAEP_PADDING, | ||
padding: crypto_1.default.constants.RSA_PKCS1_OAEP_PADDING, | ||
oaepHash: 'sha256' | ||
}; | ||
const keyBuffer = crypto.privateDecrypt(unwrapParams, encryptedKeyBuffer); | ||
const keyBuffer = crypto_1.default.privateDecrypt(unwrapParams, encryptedKeyBuffer); | ||
const aesKey = keyBuffer.toString('base64'); | ||
@@ -83,3 +88,3 @@ return aesKey; | ||
const errorCode = `JoseCrypto::DECRYPT_KEY_${error.code || 'UNKNOWN_ERR_CODE'}`; | ||
throw new JoseCryptoError(error, { errorCode }); | ||
throw new JoseCryptoError_1.default(error, { errorCode }); | ||
} | ||
@@ -103,8 +108,8 @@ } | ||
static encryptData(data, aesKey) { | ||
const keyBuffer = validateEncryptData(data, aesKey); | ||
const ivBuffer = crypto.randomBytes(16); | ||
const keyBuffer = (0, validators_1.validateEncryptData)(data, aesKey); | ||
const ivBuffer = crypto_1.default.randomBytes(16); | ||
const ivString = ivBuffer.toString('base64'); | ||
const dataString = typeof data === 'string' ? data : JSON.stringify(data); | ||
try { | ||
const encryptor = crypto.createCipheriv('aes-256-gcm', keyBuffer, ivBuffer, { authTagLength: 16 }); | ||
const encryptor = crypto_1.default.createCipheriv('aes-256-gcm', keyBuffer, ivBuffer, { authTagLength: 16 }); | ||
const cipherTextBuffer = Buffer.concat([ | ||
@@ -122,3 +127,3 @@ encryptor.update(dataString, 'utf8'), | ||
const errorCode = `JoseCrypto::ENCRYPT_DATA_${error.code || 'UNKNOWN_ERR_CODE'}`; | ||
throw new JoseCryptoError(error, { errorCode }); | ||
throw new JoseCryptoError_1.default(error, { errorCode }); | ||
} | ||
@@ -146,7 +151,7 @@ } | ||
static decryptData(payload, aesKey) { | ||
const payloadPartsBuffers = validateDecryptDataPayload(payload); | ||
const keyBuffer = validateDecryptDataKey(aesKey); | ||
const payloadPartsBuffers = (0, validators_1.validateDecryptDataPayload)(payload); | ||
const keyBuffer = (0, validators_1.validateDecryptDataKey)(aesKey); | ||
const [ivBuffer, authTagBuffer, cipherTextBuffer] = payloadPartsBuffers; | ||
try { | ||
const decryptor = crypto.createDecipheriv('aes-256-gcm', keyBuffer, ivBuffer); | ||
const decryptor = crypto_1.default.createDecipheriv('aes-256-gcm', keyBuffer, ivBuffer); | ||
decryptor.setAuthTag(authTagBuffer); | ||
@@ -158,3 +163,3 @@ const plainTextBuffer = Buffer.concat([ | ||
const plainTextString = plainTextBuffer.toString('utf8'); | ||
const data = jsonParse(plainTextString); | ||
const data = (0, utils_1.jsonParse)(plainTextString); | ||
return data; | ||
@@ -164,5 +169,6 @@ } | ||
const errorCode = `JoseCrypto::DECRYPT_DATA_${error.code || 'UNKNOWN_ERR_CODE'}`; | ||
throw new JoseCryptoError(error, { errorCode }); | ||
throw new JoseCryptoError_1.default(error, { errorCode }); | ||
} | ||
} | ||
} | ||
exports.default = JoseCrypto; |
@@ -1,2 +0,4 @@ | ||
import { SERVICE } from './CONFIG'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const CONFIG_1 = require("./CONFIG"); | ||
/** @ignore */ | ||
@@ -15,3 +17,3 @@ const DEFAULT_ERROR_MSG = 'Jose Crypto Error'; | ||
*/ | ||
export default class JoseCryptoError extends Error { | ||
class JoseCryptoError extends Error { | ||
/** | ||
@@ -54,3 +56,3 @@ * Flag to identify if error is a custom error. | ||
super(); | ||
this.service = SERVICE; | ||
this.service = CONFIG_1.SERVICE; | ||
this.message = eMap?.message || e?.message || DEFAULT_ERROR_MSG; | ||
@@ -62,1 +64,2 @@ this.statusCode = eMap?.statusCode || DEFAULT_ERROR_STATUS_CODE; | ||
} | ||
exports.default = JoseCryptoError; |
@@ -1,1 +0,2 @@ | ||
export {}; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); |
@@ -0,3 +1,6 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.derToPem = exports.jsonParse = void 0; | ||
/** @ignore */ | ||
export function jsonParse(string) { | ||
function jsonParse(string) { | ||
try { | ||
@@ -10,4 +13,5 @@ return JSON.parse(string); | ||
} | ||
exports.jsonParse = jsonParse; | ||
/** @ignore */ | ||
export function derToPem(derKey, keyType) { | ||
function derToPem(derKey, keyType) { | ||
const prefix = `-----BEGIN ${keyType}-----\n`; | ||
@@ -18,1 +22,2 @@ const postfix = `-----END ${keyType}-----`; | ||
} | ||
exports.derToPem = derToPem; |
@@ -1,36 +0,43 @@ | ||
import JoseCryptoError from './JoseCryptoError'; | ||
import { DECRYPT_DATA_AES_KEY_REQUIRED_ERROR, DECRYPT_DATA_INVALID_AES_KEY_ERROR, DECRYPT_DATA_INVALID_AES_KEY_LENGTH_ERROR, DECRYPT_DATA_INVALID_AUTHTAG_LENGTH_ERROR, DECRYPT_DATA_INVALID_IV_LENGTH_ERROR, DECRYPT_DATA_INVALID_PAYLOAD_ERROR, DECRYPT_DATA_PAYLOAD_REQUIRED_ERROR, DECRYPT_KEY_AES_KEY_REQUIRED_ERROR, DECRYPT_KEY_INVALID_AES_KEY_ERROR, DECRYPT_KEY_INVALID_RSA_KEY_ERROR, DECRYPT_KEY_RSA_KEY_REQUIRED_ERROR, ENCRYPT_DATA_AES_KEY_REQUIRED_ERROR, ENCRYPT_DATA_INVALID_AES_KEY_ERROR, ENCRYPT_DATA_INVALID_AES_KEY_LENGTH_ERROR, ENCRYPT_KEY_AES_KEY_REQUIRED_ERROR, ENCRYPT_KEY_INVALID_AES_KEY_ERROR, ENCRYPT_KEY_INVALID_AES_KEY_LENGTH_ERROR, ENCRYPT_KEY_INVALID_RSA_KEY_ERROR, ENCRYPT_KEY_RSA_KEY_REQUIRED_ERROR } from './ERRORS'; | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.validateDecryptDataKey = exports.validateDecryptDataPayload = exports.validateEncryptData = exports.validateDecryptKey = exports.validateEncryptKey = void 0; | ||
const JoseCryptoError_1 = __importDefault(require("./JoseCryptoError")); | ||
const ERRORS_1 = require("./ERRORS"); | ||
/** @ignore */ | ||
export function validateEncryptKey(aesKey, rsaPublicKey) { | ||
function validateEncryptKey(aesKey, rsaPublicKey) { | ||
if (!aesKey) { | ||
throw new JoseCryptoError(undefined, ENCRYPT_KEY_AES_KEY_REQUIRED_ERROR); | ||
throw new JoseCryptoError_1.default(undefined, ERRORS_1.ENCRYPT_KEY_AES_KEY_REQUIRED_ERROR); | ||
} | ||
if (!rsaPublicKey) { | ||
throw new JoseCryptoError(undefined, ENCRYPT_KEY_RSA_KEY_REQUIRED_ERROR); | ||
throw new JoseCryptoError_1.default(undefined, ERRORS_1.ENCRYPT_KEY_RSA_KEY_REQUIRED_ERROR); | ||
} | ||
if (typeof aesKey !== 'string') { | ||
throw new JoseCryptoError(undefined, ENCRYPT_KEY_INVALID_AES_KEY_ERROR); | ||
throw new JoseCryptoError_1.default(undefined, ERRORS_1.ENCRYPT_KEY_INVALID_AES_KEY_ERROR); | ||
} | ||
if (typeof rsaPublicKey !== 'string') { | ||
throw new JoseCryptoError(undefined, ENCRYPT_KEY_INVALID_RSA_KEY_ERROR); | ||
throw new JoseCryptoError_1.default(undefined, ERRORS_1.ENCRYPT_KEY_INVALID_RSA_KEY_ERROR); | ||
} | ||
const buffer = Buffer.from(aesKey, 'base64'); | ||
if (buffer.length !== 32) { | ||
throw new JoseCryptoError(undefined, ENCRYPT_KEY_INVALID_AES_KEY_LENGTH_ERROR); | ||
throw new JoseCryptoError_1.default(undefined, ERRORS_1.ENCRYPT_KEY_INVALID_AES_KEY_LENGTH_ERROR); | ||
} | ||
return buffer; | ||
} | ||
exports.validateEncryptKey = validateEncryptKey; | ||
/** @ignore */ | ||
export function validateDecryptKey(encryptedAesKey, rsaPrivateKey) { | ||
function validateDecryptKey(encryptedAesKey, rsaPrivateKey) { | ||
if (!encryptedAesKey) { | ||
throw new JoseCryptoError(undefined, DECRYPT_KEY_AES_KEY_REQUIRED_ERROR); | ||
throw new JoseCryptoError_1.default(undefined, ERRORS_1.DECRYPT_KEY_AES_KEY_REQUIRED_ERROR); | ||
} | ||
if (!rsaPrivateKey) { | ||
throw new JoseCryptoError(undefined, DECRYPT_KEY_RSA_KEY_REQUIRED_ERROR); | ||
throw new JoseCryptoError_1.default(undefined, ERRORS_1.DECRYPT_KEY_RSA_KEY_REQUIRED_ERROR); | ||
} | ||
if (typeof encryptedAesKey !== 'string') { | ||
throw new JoseCryptoError(undefined, DECRYPT_KEY_INVALID_AES_KEY_ERROR); | ||
throw new JoseCryptoError_1.default(undefined, ERRORS_1.DECRYPT_KEY_INVALID_AES_KEY_ERROR); | ||
} | ||
if (typeof rsaPrivateKey !== 'string') { | ||
throw new JoseCryptoError(undefined, DECRYPT_KEY_INVALID_RSA_KEY_ERROR); | ||
throw new JoseCryptoError_1.default(undefined, ERRORS_1.DECRYPT_KEY_INVALID_RSA_KEY_ERROR); | ||
} | ||
@@ -40,27 +47,29 @@ const buffer = Buffer.from(encryptedAesKey, 'base64'); | ||
} | ||
exports.validateDecryptKey = validateDecryptKey; | ||
/** @ignore */ | ||
export function validateEncryptData(data, aesKey) { | ||
function validateEncryptData(data, aesKey) { | ||
if (!aesKey) { | ||
throw new JoseCryptoError(undefined, ENCRYPT_DATA_AES_KEY_REQUIRED_ERROR); | ||
throw new JoseCryptoError_1.default(undefined, ERRORS_1.ENCRYPT_DATA_AES_KEY_REQUIRED_ERROR); | ||
} | ||
if (typeof aesKey !== 'string') { | ||
throw new JoseCryptoError(undefined, ENCRYPT_DATA_INVALID_AES_KEY_ERROR); | ||
throw new JoseCryptoError_1.default(undefined, ERRORS_1.ENCRYPT_DATA_INVALID_AES_KEY_ERROR); | ||
} | ||
const buffer = Buffer.from(aesKey, 'base64'); | ||
if (buffer.length !== 32) { | ||
throw new JoseCryptoError(undefined, ENCRYPT_DATA_INVALID_AES_KEY_LENGTH_ERROR); | ||
throw new JoseCryptoError_1.default(undefined, ERRORS_1.ENCRYPT_DATA_INVALID_AES_KEY_LENGTH_ERROR); | ||
} | ||
return buffer; | ||
} | ||
exports.validateEncryptData = validateEncryptData; | ||
/** @ignore */ | ||
export function validateDecryptDataPayload(payload) { | ||
function validateDecryptDataPayload(payload) { | ||
if (!payload) { | ||
throw new JoseCryptoError(undefined, DECRYPT_DATA_PAYLOAD_REQUIRED_ERROR); | ||
throw new JoseCryptoError_1.default(undefined, ERRORS_1.DECRYPT_DATA_PAYLOAD_REQUIRED_ERROR); | ||
} | ||
if (typeof payload !== 'string') { | ||
throw new JoseCryptoError(undefined, DECRYPT_DATA_INVALID_PAYLOAD_ERROR); | ||
throw new JoseCryptoError_1.default(undefined, ERRORS_1.DECRYPT_DATA_INVALID_PAYLOAD_ERROR); | ||
} | ||
const [ivString, authTagString, cipherTextString] = payload.split('.'); | ||
if (!ivString || !authTagString || !cipherTextString) { | ||
throw new JoseCryptoError(undefined, DECRYPT_DATA_INVALID_PAYLOAD_ERROR); | ||
throw new JoseCryptoError_1.default(undefined, ERRORS_1.DECRYPT_DATA_INVALID_PAYLOAD_ERROR); | ||
} | ||
@@ -71,22 +80,24 @@ const ivBuffer = Buffer.from(ivString, 'base64'); | ||
if (ivBuffer.length !== 16) { | ||
throw new JoseCryptoError(undefined, DECRYPT_DATA_INVALID_IV_LENGTH_ERROR); | ||
throw new JoseCryptoError_1.default(undefined, ERRORS_1.DECRYPT_DATA_INVALID_IV_LENGTH_ERROR); | ||
} | ||
if (authTagBuffer.length !== 16) { | ||
throw new JoseCryptoError(undefined, DECRYPT_DATA_INVALID_AUTHTAG_LENGTH_ERROR); | ||
throw new JoseCryptoError_1.default(undefined, ERRORS_1.DECRYPT_DATA_INVALID_AUTHTAG_LENGTH_ERROR); | ||
} | ||
return [ivBuffer, authTagBuffer, cipherTextBuffer]; | ||
} | ||
exports.validateDecryptDataPayload = validateDecryptDataPayload; | ||
/** @ignore */ | ||
export function validateDecryptDataKey(aesKey) { | ||
function validateDecryptDataKey(aesKey) { | ||
if (!aesKey) { | ||
throw new JoseCryptoError(undefined, DECRYPT_DATA_AES_KEY_REQUIRED_ERROR); | ||
throw new JoseCryptoError_1.default(undefined, ERRORS_1.DECRYPT_DATA_AES_KEY_REQUIRED_ERROR); | ||
} | ||
if (typeof aesKey !== 'string') { | ||
throw new JoseCryptoError(undefined, DECRYPT_DATA_INVALID_AES_KEY_ERROR); | ||
throw new JoseCryptoError_1.default(undefined, ERRORS_1.DECRYPT_DATA_INVALID_AES_KEY_ERROR); | ||
} | ||
const buffer = Buffer.from(aesKey, 'base64'); | ||
if (buffer.length !== 32) { | ||
throw new JoseCryptoError(undefined, DECRYPT_DATA_INVALID_AES_KEY_LENGTH_ERROR); | ||
throw new JoseCryptoError_1.default(undefined, ERRORS_1.DECRYPT_DATA_INVALID_AES_KEY_LENGTH_ERROR); | ||
} | ||
return buffer; | ||
} | ||
exports.validateDecryptDataKey = validateDecryptDataKey; |
{ | ||
"name": "@am92/jose-crypto", | ||
"version": "2.0.2", | ||
"version": "2.0.3", | ||
"description": "JOSE Cryptography", | ||
@@ -5,0 +5,0 @@ "sideEffects": false, |
33167
708