Skip to content

Commit

Permalink
feat: added seed cipher algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
FajarKim committed Jan 2, 2024
1 parent ef430ad commit 1f4f41d
Show file tree
Hide file tree
Showing 10 changed files with 408 additions and 6 deletions.
12 changes: 12 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ const {
encryptIDAES256CCM, decryptIDAES256CCM,
encryptIDAES256GCM, decryptIDAES256GCM
} = require("./lib/id/index");
const {
encryptSEED, decryptSEED,
encryptSEEDCBC, decryptSEEDCBC,
encryptSEEDCFB, decryptSEEDCFB,
encryptSEEDECB, decryptSEEDECB,
encryptSEEDOFB, decryptSEEDOFB
} = require("./lib/seed/index");
const {
encryptSM4, decryptSM4,
encryptSM4CBC, decryptSM4CBC,
Expand Down Expand Up @@ -259,6 +266,11 @@ module.exports = {
encryptIDAES192GCM, decryptIDAES192GCM,
encryptIDAES256CCM, decryptIDAES256CCM,
encryptIDAES256GCM, decryptIDAES256GCM,
encryptSEED, decryptSEED,
encryptSEEDCBC, decryptSEEDCBC,
encryptSEEDCFB, decryptSEEDCFB,
encryptSEEDECB, decryptSEEDECB,
encryptSEEDOFB, decryptSEEDOFB,
encryptSM4, decryptSM4,
encryptSM4CBC, decryptSM4CBC,
encryptSM4CFB, decryptSM4CFB,
Expand Down
12 changes: 6 additions & 6 deletions lib/algorithmList.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ const algorithmSupported = [
"id-aes192-gcm",
"id-aes256-ccm",
"id-aes256-gcm",
"seed",
"seed-cbc",
"seed-cfb",
"seed-ecb",
"seed-ofb",
"sm4",
"sm4-cbc",
"sm4-cfb",
Expand Down Expand Up @@ -174,12 +179,7 @@ const algorithmUnsupported = [
"rc2-64",
"rc2-64-cbc",
"rc4-cbc",
"rc4-40",
"seed",
"seed-cbc",
"seed-cfb",
"seed-ecb",
"seed-ofb"
"rc4-40"
];

module.exports = {
Expand Down
22 changes: 22 additions & 0 deletions lib/decrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ const {
decryptIDAES256CCM,
decryptIDAES256GCM
} = require("./id/index");
const {
decryptSEED,
decryptSEEDCBC,
decryptSEEDCFB,
decryptSEEDECB,
decryptSEEDOFB
} = require("./seed/index");
const {
decryptSM4,
decryptSM4CBC,
Expand Down Expand Up @@ -470,6 +477,21 @@ function decrypt(algorithm, data, password, extra = null) {
return decryptIDAES256GCM(data, password, extra);
}

/*****************************************************
* SEED *
*****************************************************/
if (algorithm === "seed") {
return decryptSEED(data, password, extra);
} else if (algorithm === "seed-cbc") {
return decryptSEEDCBC(data, password, extra);
} else if (algorithm === "seed-cfb") {
return decryptSEEDCFB(data, password, extra);
} else if (algorithm === "seed-ecb") {
return decryptSEEDECB(data, password, extra);
} else if (algorithm === "seed-ofb") {
return decryptSEEDOFB(data, password, extra);
}

/*****************************************************
* SM4 *
*****************************************************/
Expand Down
22 changes: 22 additions & 0 deletions lib/encrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ const {
encryptIDAES256CCM,
encryptIDAES256GCM
} = require("./id/index");
const {
encryptSEED,
encryptSEEDCBC,
encryptSEEDCFB,
encryptSEEDECB,
encryptSEEDOFB
} = require("./seed/index");
const {
encryptSM4,
encryptSM4CBC,
Expand Down Expand Up @@ -470,6 +477,21 @@ function encrypt(algorithm, data, password, extra = null) {
return encryptIDAES256GCM(data, password, extra);
}

/*****************************************************
* SEED *
*****************************************************/
if (algorithm === "seed") {
return encryptSEED(data, password, extra);
} else if (algorithm === "seed-cbc") {
return encryptSEEDCBC(data, password, extra);
} else if (algorithm === "seed-cfb") {
return encryptSEEDCFB(data, password, extra);
} else if (algorithm === "seed-ecb") {
return encryptSEEDECB(data, password, extra);
} else if (algorithm === "seed-ofb") {
return encryptSEEDOFB(data, password, extra);
}

/*****************************************************
* SM4 *
*****************************************************/
Expand Down
13 changes: 13 additions & 0 deletions lib/seed/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { encryptSEED, decryptSEED } = require("./seed");
const { encryptSEEDCBC, decryptSEEDCBC } = require("./seed-cbc");
const { encryptSEEDCFB, decryptSEEDCFB } = require("./seed-cfb");
const { encryptSEEDECB, decryptSEEDECB } = require("./seed-ecb");
const { encryptSEEDOFB, decryptSEEDOFB } = require("./seed-ofb");

module.exports = {
encryptSEED, decryptSEED,
encryptSEEDCBC, decryptSEEDCBC,
encryptSEEDCFB, decryptSEEDCFB,
encryptSEEDECB, decryptSEEDECB,
encryptSEEDOFB, decryptSEEDOFB
};
67 changes: 67 additions & 0 deletions lib/seed/seed-cbc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const crypto = require("crypto");
const zlib = require("zlib");

/**
* Encrypts data using the SEED-CBC algorithm.
*
* @param {string} data - The data to be encrypted.
* @param {string} password - The password used for key derivation.
* @param {string|null} extra - Additional options for encryption (e.g., 'base64', 'binary', 'hex', or 'zlib').
* @returns {Buffer|string} - The encrypted data.
*/
function encryptSEEDCBC(data, password, extra = null) {
const salt = crypto.randomBytes(8);
const iv = crypto.randomBytes(16);
const key = crypto.pbkdf2Sync(password, salt, 100000, 16, "sha256");
const cipher = crypto.createCipheriv("seed-cbc", key, iv);
var encrypted = Buffer.concat([Buffer.from("Salted__"), salt, iv, cipher.update(data, "utf-8"), cipher.final()]);
if (extra === "base64") {
var encrypted = encrypted.toString("base64");
} else if (extra === "binary") {
var encrypted = encrypted.toString("binary");
} else if (extra === "hex") {
var encrypted = encrypted.toString("hex");
} else if (extra === "zlib") {
var encrypted = zlib.deflateSync(encrypted);
}

return encrypted;
}

/**
* Decrypts data using the SEED-CBC algorithm.
*
* @param {Buffer|string} data - The data to be decrypted.
* @param {string} password - The password used for key derivation.
* @param {string|null} extra - Additional options for decryption (e.g., 'base64', 'binary', 'hex', or 'zlib').
* @returns {string} - The decrypted data.
*/
function decryptSEEDCBC(encryptedData, password, extra = null) {
var data = encryptedData;
if (extra === "base64") {
var data = Buffer.from(data, "base64");
} else if (extra === "binary") {
var data = Buffer.from(data, "binary");
} else if (extra === "hex") {
var data = Buffer.from(data, "hex");
} else if (extra === "zlib") {
var data = zlib.inflateSync(data);
}

if (! Buffer.isBuffer(data)) {
var buffString = data;
var hexValues = buffString.replace(/<Buffer|>/g, "").split(" ").filter(Boolean);
var bufferArray = hexValues.map(hex => parseInt(hex, 16));
var data = Buffer.from(bufferArray);
}

const salt = data.slice(8, 16);
const iv = data.slice(16, 32);
const key = crypto.pbkdf2Sync(password, salt, 100000, 16, "sha256");
const decipher = crypto.createDecipheriv("seed-cbc", key, iv);
const decrypted = Buffer.concat([decipher.update(data.slice(32)), decipher.final()]).toString("utf-8");

return decrypted;
}

module.exports = { encryptSEEDCBC, decryptSEEDCBC };
67 changes: 67 additions & 0 deletions lib/seed/seed-cfb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const crypto = require("crypto");
const zlib = require("zlib");

/**
* Encrypts data using the SEED-CFB algorithm.
*
* @param {string} data - The data to be encrypted.
* @param {string} password - The password used for key derivation.
* @param {string|null} extra - Additional options for encryption (e.g., 'base64', 'binary', 'hex', or 'zlib').
* @returns {Buffer|string} - The encrypted data.
*/
function encryptSEEDCFB(data, password, extra = null) {
const salt = crypto.randomBytes(8);
const iv = crypto.randomBytes(16);
const key = crypto.pbkdf2Sync(password, salt, 100000, 16, "sha256");
const cipher = crypto.createCipheriv("seed-cfb", key, iv);
var encrypted = Buffer.concat([Buffer.from("Salted__"), salt, iv, cipher.update(data, "utf-8"), cipher.final()]);
if (extra === "base64") {
var encrypted = encrypted.toString("base64");
} else if (extra === "binary") {
var encrypted = encrypted.toString("binary");
} else if (extra === "hex") {
var encrypted = encrypted.toString("hex");
} else if (extra === "zlib") {
var encrypted = zlib.deflateSync(encrypted);
}

return encrypted;
}

/**
* Decrypts data using the SEED-CFB algorithm.
*
* @param {Buffer|string} data - The data to be decrypted.
* @param {string} password - The password used for key derivation.
* @param {string|null} extra - Additional options for decryption (e.g., 'base64', 'binary', 'hex', or 'zlib').
* @returns {string} - The decrypted data.
*/
function decryptSEEDCFB(encryptedData, password, extra = null) {
var data = encryptedData;
if (extra === "base64") {
var data = Buffer.from(data, "base64");
} else if (extra === "binary") {
var data = Buffer.from(data, "binary");
} else if (extra === "hex") {
var data = Buffer.from(data, "hex");
} else if (extra === "zlib") {
var data = zlib.inflateSync(data);
}

if (! Buffer.isBuffer(data)) {
var buffString = data;
var hexValues = buffString.replace(/<Buffer|>/g, "").split(" ").filter(Boolean);
var bufferArray = hexValues.map(hex => parseInt(hex, 16));
var data = Buffer.from(bufferArray);
}

const salt = data.slice(8, 16);
const iv = data.slice(16, 32);
const key = crypto.pbkdf2Sync(password, salt, 100000, 16, "sha256");
const decipher = crypto.createDecipheriv("seed-cfb", key, iv);
const decrypted = Buffer.concat([decipher.update(data.slice(32)), decipher.final()]).toString("utf-8");

return decrypted;
}

module.exports = { encryptSEEDCFB, decryptSEEDCFB };
65 changes: 65 additions & 0 deletions lib/seed/seed-ecb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const crypto = require("crypto");
const zlib = require("zlib");

/**
* Encrypts data using the SEED-ECB algorithm.
*
* @param {string} data - The data to be encrypted.
* @param {string} password - The password used for key derivation.
* @param {string|null} extra - Additional options for encryption (e.g., 'base64', 'binary', 'hex', or 'zlib').
* @returns {Buffer|string} - The encrypted data.
*/
function encryptSEEDECB(data, password, extra = null) {
const salt = crypto.randomBytes(8);
const key = crypto.pbkdf2Sync(password, salt, 100000, 16, "sha256");
const cipher = crypto.createCipheriv("seed-ecb", key, null);
var encrypted = Buffer.concat([Buffer.from("Salted__"), salt, cipher.update(data, "utf-8"), cipher.final()]);
if (extra === "base64") {
var encrypted = encrypted.toString("base64");
} else if (extra === "binary") {
var encrypted = encrypted.toString("binary");
} else if (extra === "hex") {
var encrypted = encrypted.toString("hex");
} else if (extra === "zlib") {
var encrypted = zlib.deflateSync(encrypted);
}

return encrypted;
}

/**
* Decrypts data using the SEED-ECB algorithm.
*
* @param {Buffer|string} data - The data to be decrypted.
* @param {string} password - The password used for key derivation.
* @param {string|null} extra - Additional options for decryption (e.g., 'base64', 'binary', 'hex', or 'zlib').
* @returns {string} - The decrypted data.
*/
function decryptSEEDECB(encryptedData, password, extra = null) {
var data = encryptedData;
if (extra === "base64") {
var data = Buffer.from(data, "base64");
} else if (extra === "binary") {
var data = Buffer.from(data, "binary");
} else if (extra === "hex") {
var data = Buffer.from(data, "hex");
} else if (extra === "zlib") {
var data = zlib.inflateSync(data);
}

if (! Buffer.isBuffer(data)) {
var buffString = data;
var hexValues = buffString.replace(/<Buffer|>/g, "").split(" ").filter(Boolean);
var bufferArray = hexValues.map(hex => parseInt(hex, 16));
var data = Buffer.from(bufferArray);
}

const salt = data.slice(8, 16);
const key = crypto.pbkdf2Sync(password, salt, 100000, 16, "sha256");
const decipher = crypto.createDecipheriv("seed-ecb", key, null);
const decrypted = Buffer.concat([decipher.update(data.slice(16)), decipher.final()]).toString("utf-8");

return decrypted;
}

module.exports = { encryptSEEDECB, decryptSEEDECB };
Loading

0 comments on commit 1f4f41d

Please sign in to comment.