mirror of https://github.com/waku-org/js-waku.git
Use browser (subtle) implementation for all env
This commit is contained in:
parent
2e816918c3
commit
9dae5168fc
|
@ -1,38 +1,37 @@
|
||||||
export const SymmetricKeySize = 32;
|
import { randomBytes, subtle } from "../../crypto";
|
||||||
|
|
||||||
|
export const KeySize = 32;
|
||||||
export const IvSize = 12;
|
export const IvSize = 12;
|
||||||
export const TagSize = 16;
|
export const TagSize = 16;
|
||||||
|
|
||||||
export interface Symmetric {
|
const Algorithm = { name: "AES-GCM", length: 128 };
|
||||||
/**
|
|
||||||
* Proceed with symmetric encryption of `clearText` value.
|
export async function encrypt(
|
||||||
*/
|
|
||||||
encrypt: (
|
|
||||||
iv: Buffer | Uint8Array,
|
iv: Buffer | Uint8Array,
|
||||||
key: Buffer,
|
key: Buffer,
|
||||||
clearText: Buffer
|
clearText: Buffer
|
||||||
) => Promise<Buffer>;
|
): Promise<Buffer> {
|
||||||
/**
|
return subtle
|
||||||
* Proceed with symmetric decryption of `cipherText` value.
|
.importKey("raw", key, Algorithm, false, ["encrypt"])
|
||||||
*/
|
.then((cryptoKey) =>
|
||||||
decrypt: (iv: Buffer, key: Buffer, cipherText: Buffer) => Promise<Buffer>;
|
subtle.encrypt({ iv, ...Algorithm }, cryptoKey, clearText)
|
||||||
/**
|
)
|
||||||
* Generate an Initialization Vector (iv) for for Symmetric encryption purposes.
|
.then(Buffer.from);
|
||||||
*/
|
|
||||||
generateIv: () => Uint8Array;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export let symmetric: Symmetric = {} as unknown as Symmetric;
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
import("./browser")
|
export function generateIv(): Uint8Array {
|
||||||
.then((mod) => {
|
return randomBytes(IvSize);
|
||||||
symmetric = mod;
|
}
|
||||||
})
|
|
||||||
.catch((eBrowser) => {
|
|
||||||
import("./node")
|
|
||||||
.then((mod) => {
|
|
||||||
symmetric = mod;
|
|
||||||
})
|
|
||||||
.catch((eNode) => {
|
|
||||||
throw `Could not load any symmetric crypto modules: ${eBrowser}, ${eNode}`;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
|
@ -7,7 +7,7 @@ import * as secp256k1 from "secp256k1";
|
||||||
import { hexToBytes } from "../utils";
|
import { hexToBytes } from "../utils";
|
||||||
|
|
||||||
import * as ecies from "./ecies";
|
import * as ecies from "./ecies";
|
||||||
import { IvSize, symmetric, SymmetricKeySize } from "./symmetric";
|
import * as symmetric from "./symmetric";
|
||||||
|
|
||||||
const FlagsLength = 1;
|
const FlagsLength = 1;
|
||||||
const FlagMask = 3; // 0011
|
const FlagMask = 3; // 0011
|
||||||
|
@ -170,7 +170,7 @@ export async function decryptSymmetric(
|
||||||
key: Uint8Array | Buffer | string
|
key: Uint8Array | Buffer | string
|
||||||
): Promise<Uint8Array> {
|
): Promise<Uint8Array> {
|
||||||
const data = Buffer.from(payload);
|
const data = Buffer.from(payload);
|
||||||
const ivStart = data.length - IvSize;
|
const ivStart = data.length - symmetric.IvSize;
|
||||||
const cipher = data.slice(0, ivStart);
|
const cipher = data.slice(0, ivStart);
|
||||||
const iv = data.slice(ivStart);
|
const iv = data.slice(ivStart);
|
||||||
|
|
||||||
|
@ -190,7 +190,7 @@ export function generatePrivateKey(): Uint8Array {
|
||||||
* Generate a new symmetric key to be used for symmetric encryption.
|
* Generate a new symmetric key to be used for symmetric encryption.
|
||||||
*/
|
*/
|
||||||
export function generateSymmetricKey(): Uint8Array {
|
export function generateSymmetricKey(): Uint8Array {
|
||||||
return randomBytes(SymmetricKeySize);
|
return randomBytes(symmetric.KeySize);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue