Remove node implementation

This commit is contained in:
Franck Royer 2022-03-07 07:50:33 +11:00
parent 9dae5168fc
commit 111f31bb53
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
3 changed files with 1 additions and 88 deletions

View File

@ -1,4 +1,4 @@
import { randomBytes, subtle } from "../../crypto";
import { randomBytes, subtle } from "../crypto";
export const KeySize = 32;
export const IvSize = 12;

View File

@ -1,51 +0,0 @@
import { IvSize } from "./index";
declare global {
interface Window {
msCrypto?: Crypto;
}
interface Crypto {
webkitSubtle?: SubtleCrypto;
}
}
const crypto = window.crypto || window.msCrypto;
const subtle: SubtleCrypto = crypto.subtle || crypto.webkitSubtle;
const Algorithm = { name: "AES-GCM", length: 128 };
if (subtle === undefined) {
throw new Error("Failed to load Subtle CryptoAPI");
}
export async function encrypt(
iv: Buffer | Uint8Array,
key: Buffer,
clearText: Buffer
): Promise<Buffer> {
return subtle
.importKey("raw", key, Algorithm, false, ["encrypt"])
.then((cryptoKey) =>
subtle.encrypt({ iv, ...Algorithm }, cryptoKey, clearText)
)
.then(Buffer.from);
}
export async function decrypt(
iv: Buffer,
key: Buffer,
cipherText: Buffer
): Promise<Buffer> {
return subtle
.importKey("raw", key, Algorithm, false, ["decrypt"])
.then((cryptoKey) =>
subtle.decrypt({ iv, ...Algorithm }, cryptoKey, cipherText)
)
.then(Buffer.from);
}
export function generateIv(): Uint8Array {
const iv = new Uint8Array(IvSize);
crypto.getRandomValues(iv);
return iv;
}

View File

@ -1,36 +0,0 @@
import { createCipheriv, createDecipheriv, randomBytes } from "crypto";
import { IvSize, TagSize } from "./index";
const Algorithm = "aes-256-gcm";
export async function encrypt(
iv: Buffer | Uint8Array,
key: Buffer,
clearText: Buffer
): Promise<Buffer> {
const cipher = createCipheriv(Algorithm, key, iv);
const a = cipher.update(clearText);
const b = cipher.final();
const tag = cipher.getAuthTag();
return Buffer.concat([a, b, tag]);
}
export async function decrypt(
iv: Buffer,
key: Buffer,
data: Buffer
): Promise<Buffer> {
const tagStart = data.length - TagSize;
const cipherText = data.slice(0, tagStart);
const tag = data.slice(tagStart);
const decipher = createDecipheriv(Algorithm, key, iv);
decipher.setAuthTag(tag);
const a = decipher.update(cipherText);
const b = decipher.final();
return Buffer.concat([a, b]);
}
export function generateIv(): Buffer {
return randomBytes(IvSize);
}