-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added seed cipher algorithm (#13)
- Loading branch information
Showing
10 changed files
with
408 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
Oops, something went wrong.