yes encryption!

This commit is contained in:
fyears 2021-11-02 02:39:24 +08:00
parent 010561596e
commit a9d80bef8f
3 changed files with 79 additions and 48 deletions

View File

@ -13,6 +13,7 @@
"author": "", "author": "",
"license": "Apache-2.0", "license": "Apache-2.0",
"devDependencies": { "devDependencies": {
"@types/crypto-js": "^4.0.2",
"@types/node": "^14.14.37", "@types/node": "^14.14.37",
"prettier": "^2.4.1", "prettier": "^2.4.1",
"terser-webpack-plugin": "^5.2.4", "terser-webpack-plugin": "^5.2.4",
@ -36,6 +37,7 @@
"console-browserify": "^1.2.0", "console-browserify": "^1.2.0",
"constants-browserify": "^1.0.0", "constants-browserify": "^1.0.0",
"crypto-browserify": "^3.12.0", "crypto-browserify": "^3.12.0",
"crypto-js": "^4.1.1",
"domain-browser": "^4.22.0", "domain-browser": "^4.22.0",
"events": "^3.3.0", "events": "^3.3.0",
"hi-base32": "^0.5.1", "hi-base32": "^0.5.1",

View File

@ -1,54 +1,65 @@
import * as crypto from "crypto"; import * as CryptoJS from "crypto-js";
import * as base32 from "hi-base32"; import * as base32 from "hi-base32";
import { bufferToArrayBuffer, arrayBufferToBuffer } from "./misc"; import {
bufferToArrayBuffer,
arrayBufferToBuffer,
arrayBufferToBase64,
base64ToArrayBuffer,
} from "./misc";
const DEFAULT_ITER = 10000; const DEFAULT_ITER = 10000;
export const encryptBuffer = ( export const encryptWordArray = (
buf: Buffer, wa: CryptoJS.lib.WordArray,
password: string, password: string,
rounds: number = DEFAULT_ITER rounds: number = DEFAULT_ITER
) => { ) => {
const salt = crypto.randomBytes(8); const prefix = CryptoJS.enc.Utf8.parse("Salted__");
const derivedKey = crypto.pbkdf2Sync( const salt = CryptoJS.lib.WordArray.random(8);
password, const derivedKey = CryptoJS.PBKDF2(password, salt, {
salt, keySize: 32 + 16,
rounds, iterations: rounds,
32 + 16, hasher: CryptoJS.algo.SHA256,
"sha256" });
const key = CryptoJS.lib.WordArray.create(derivedKey.words.slice(0, 32 / 4));
const iv = CryptoJS.lib.WordArray.create(
derivedKey.words.slice(32 / 4, (32 + 16) / 4)
); );
const key = derivedKey.slice(0, 32); const encrypted = CryptoJS.AES.encrypt(wa, key, { iv: iv }).ciphertext;
const iv = derivedKey.slice(32, 32 + 16); const res = CryptoJS.lib.WordArray.create()
const cipher = crypto.createCipheriv("aes-256-cbc", key, iv); .concat(prefix)
cipher.write(buf); .concat(salt)
cipher.end(); .concat(encrypted);
const encrypted = cipher.read();
const res = Buffer.concat([Buffer.from("Salted__"), salt, encrypted]);
return res; return res;
}; };
export const decryptBuffer = ( export const decryptWordArray = (
buf: Buffer, wa: CryptoJS.lib.WordArray,
password: string, password: string,
rounds: number = DEFAULT_ITER rounds: number = DEFAULT_ITER
) => { ) => {
const prefix = buf.slice(0, 8); const prefix = CryptoJS.lib.WordArray.create(wa.words.slice(0, 8 / 4));
const salt = buf.slice(8, 16);
const derivedKey = crypto.pbkdf2Sync( const salt = CryptoJS.lib.WordArray.create(
password, wa.words.slice(8 / 4, (8 + 8) / 4)
salt,
rounds,
32 + 16,
"sha256"
); );
const key = derivedKey.slice(0, 32); const derivedKey = CryptoJS.PBKDF2(password, salt, {
const iv = derivedKey.slice(32, 32 + 16); keySize: 32 + 16,
const decipher = crypto.createDecipheriv("aes-256-cbc", key, iv); iterations: rounds,
decipher.write(buf.slice(16)); hasher: CryptoJS.algo.SHA256,
decipher.end(); });
const decrypted = decipher.read(); const key = CryptoJS.lib.WordArray.create(derivedKey.words.slice(0, 32 / 4));
return decrypted as Buffer; const iv = CryptoJS.lib.WordArray.create(
derivedKey.words.slice(32 / 4, 32 / 4 + 16 / 4)
);
const decrypted = CryptoJS.AES.decrypt(
CryptoJS.lib.CipherParams.create({
ciphertext: CryptoJS.lib.WordArray.create(wa.words.slice((8 + 8) / 4)),
}),
key,
{ iv: iv }
);
return decrypted;
}; };
export const encryptArrayBuffer = ( export const encryptArrayBuffer = (
@ -56,9 +67,12 @@ export const encryptArrayBuffer = (
password: string, password: string,
rounds: number = DEFAULT_ITER rounds: number = DEFAULT_ITER
) => { ) => {
return bufferToArrayBuffer( const b64 = arrayBufferToBase64(arrBuf);
encryptBuffer(arrayBufferToBuffer(arrBuf), password, rounds) const wa = CryptoJS.enc.Base64.parse(b64);
); const enc = encryptWordArray(wa, password, rounds);
const resb64 = CryptoJS.enc.Base64.stringify(enc);
const res = base64ToArrayBuffer(resb64);
return res;
}; };
export const decryptArrayBuffer = ( export const decryptArrayBuffer = (
@ -66,9 +80,12 @@ export const decryptArrayBuffer = (
password: string, password: string,
rounds: number = DEFAULT_ITER rounds: number = DEFAULT_ITER
) => { ) => {
return bufferToArrayBuffer( const b64 = arrayBufferToBase64(arrBuf);
decryptBuffer(arrayBufferToBuffer(arrBuf), password, rounds) const wa = CryptoJS.enc.Base64.parse(b64);
); const dec = decryptWordArray(wa, password, rounds);
const resb64 = CryptoJS.enc.Base64.stringify(dec);
const res = base64ToArrayBuffer(resb64);
return res;
}; };
export const encryptStringToBase32 = ( export const encryptStringToBase32 = (
@ -76,7 +93,11 @@ export const encryptStringToBase32 = (
password: string, password: string,
rounds: number = DEFAULT_ITER rounds: number = DEFAULT_ITER
) => { ) => {
return base32.encode(encryptBuffer(Buffer.from(text), password, rounds)); const wa = CryptoJS.enc.Utf8.parse(text);
const enc = encryptWordArray(wa, password, rounds);
const enctext = CryptoJS.enc.Base64.stringify(enc);
const res = base32.encode(base64ToArrayBuffer(enctext));
return res;
}; };
export const decryptBase32ToString = ( export const decryptBase32ToString = (
@ -84,9 +105,9 @@ export const decryptBase32ToString = (
password: string, password: string,
rounds: number = DEFAULT_ITER rounds: number = DEFAULT_ITER
) => { ) => {
return decryptBuffer( const enc = Buffer.from(base32.decode.asBytes(text)).toString("base64");
Buffer.from(base32.decode.asBytes(text)), const wa = CryptoJS.enc.Base64.parse(enc);
password, const dec = decryptWordArray(wa, password, rounds);
rounds const dectext = CryptoJS.enc.Utf8.stringify(dec);
).toString(); return dectext;
}; };

View File

@ -58,3 +58,11 @@ export const bufferToArrayBuffer = (b: Buffer) => {
export const arrayBufferToBuffer = (b: ArrayBuffer) => { export const arrayBufferToBuffer = (b: ArrayBuffer) => {
return Buffer.from(b); return Buffer.from(b);
}; };
export const arrayBufferToBase64 = (b: ArrayBuffer) => {
return arrayBufferToBuffer(b).toString("base64");
};
export const base64ToArrayBuffer = (b64text: string) => {
return bufferToArrayBuffer(Buffer.from(b64text, "base64"));
};