UNPKG

@peculiar/webcrypto

Version:
87 lines (86 loc) • 3.35 kB
/** * Copyright (c) 2019-2026, Peculiar Ventures * SPDX-License-Identifier: MIT */ import { DesCryptoKey } from "./key.js"; import * as core from "webcrypto-core"; import { Buffer } from "node:buffer"; import crypto from "node:crypto"; import { JsonParser, JsonSerializer } from "@peculiar/json-schema"; import { assertBufferSource, toArrayBuffer, toUint8Array } from "@peculiar/utils"; // src/mechs/des/crypto.ts var DesCrypto = class { static async generateKey(algorithm, extractable, keyUsages) { const key = new DesCryptoKey(); key.algorithm = algorithm; key.extractable = extractable; key.usages = keyUsages; key.data = crypto.randomBytes(algorithm.length >> 3); return key; } static async exportKey(format, key) { switch (format.toLowerCase()) { case "jwk": return JsonSerializer.toJSON(key); case "raw": return toArrayBuffer(key.data); default: throw new core.OperationError("format: Must be 'jwk' or 'raw'"); } } static async importKey(format, keyData, algorithm, extractable, keyUsages) { let key; switch (format.toLowerCase()) { case "jwk": key = JsonParser.fromJSON(keyData, { targetSchema: DesCryptoKey }); break; case "raw": assertBufferSource(keyData); key = new DesCryptoKey(); key.data = Buffer.from(toUint8Array(keyData)); break; default: throw new core.OperationError("format: Must be 'jwk' or 'raw'"); } key.algorithm = algorithm; key.extractable = extractable; key.usages = keyUsages; return key; } static async encrypt(algorithm, key, data) { switch (algorithm.name.toUpperCase()) { case "DES-CBC": return this.encryptDesCBC(algorithm, key, Buffer.from(data)); case "DES-EDE3-CBC": return this.encryptDesEDE3CBC(algorithm, key, Buffer.from(data)); default: throw new core.OperationError("algorithm: Is not recognized"); } } static async decrypt(algorithm, key, data) { if (!(key instanceof DesCryptoKey)) throw new Error("key: Is not DesCryptoKey"); switch (algorithm.name.toUpperCase()) { case "DES-CBC": return this.decryptDesCBC(algorithm, key, Buffer.from(data)); case "DES-EDE3-CBC": return this.decryptDesEDE3CBC(algorithm, key, Buffer.from(data)); default: throw new core.OperationError("algorithm: Is not recognized"); } } static async encryptDesCBC(algorithm, key, data) { const cipher = crypto.createCipheriv("des-cbc", key.data, toUint8Array(algorithm.iv)); let enc = cipher.update(data); enc = Buffer.concat([enc, cipher.final()]); return toArrayBuffer(enc); } static async decryptDesCBC(algorithm, key, data) { const decipher = crypto.createDecipheriv("des-cbc", key.data, toUint8Array(algorithm.iv)); let dec = decipher.update(data); dec = Buffer.concat([dec, decipher.final()]); return toArrayBuffer(dec); } static async encryptDesEDE3CBC(algorithm, key, data) { const cipher = crypto.createCipheriv("des-ede3-cbc", key.data, Buffer.from(toUint8Array(algorithm.iv))); let enc = cipher.update(data); enc = Buffer.concat([enc, cipher.final()]); return toArrayBuffer(enc); } static async decryptDesEDE3CBC(algorithm, key, data) { const decipher = crypto.createDecipheriv("des-ede3-cbc", key.data, toUint8Array(algorithm.iv)); let dec = decipher.update(data); dec = Buffer.concat([dec, decipher.final()]); return toArrayBuffer(dec); } }; export { DesCrypto };