UNPKG

aes-universal-node

Version:
2 lines (1 loc) 1.74 kB
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const u=require("aes-universal"),i=require("crypto"),p=a=>{switch(a){case 128:return"aes-128-gcm";case 192:return"aes-192-gcm";case 256:return"aes-256-gcm";default:throw new Error(`Invalid key bit length: ${a}`)}};class d extends u.AbstractCbcCipher{constructor(){super(...arguments),this.encryptInternal=async({encRawKey:e,iv:c,plaintext:n})=>{const s=e.length*8,t=i.createCipheriv(`aes-${s}-cbc`,e,c),r=Buffer.concat([t.update(n),t.final()]);return new Uint8Array(r)},this.decryptInternal=async({encRawKey:e,iv:c,ciphertext:n})=>{const s=e.length*8,t=i.createDecipheriv(`aes-${s}-cbc`,e,c),r=Buffer.concat([t.update(n),t.final()]);return new Uint8Array(r)},this.generateTag=async({macRawKey:e,macData:c,keyBitLength:n})=>{const s=`sha${n<<1}`,t=i.createHmac(s,e);return t.update(c),new Uint8Array(t.digest()).slice(0,n>>>3)}}}class l extends u.AbstractGcmCipher{constructor(){super(...arguments),this.encryptInternal=async({encRawKey:e,iv:c,plaintext:n,aad:s})=>{const t=p(e.length<<3),r=i.createCipheriv(t,e,c);r.setAAD(s);const o=Buffer.concat([r.update(n),r.final()]),h=r.getAuthTag();return{ciphertext:new Uint8Array(o),tag:new Uint8Array(h)}},this.decryptInternal=async({encRawKey:e,iv:c,ciphertext:n,tag:s,aad:t})=>{const r=p(e.length<<3),o=i.createDecipheriv(r,e,c);o.setAAD(t),o.setAuthTag(s);const h=Buffer.concat([o.update(n),o.final()]);return new Uint8Array(h)}}}class g extends u.AesCipher{constructor(){super({cbc:new d,gcm:new l})}}const y=new g,A=(a=32)=>new Uint8Array(i.randomBytes(a));exports.NodeAesCipher=g;exports.NodeCbcCipher=d;exports.NodeGcmCipher=l;exports.keyBitLengthToGCMType=p;exports.nodeAesCipher=y;exports.nodeRandomBytes=A;