feistel-cipher
Advanced tools
Comparing version 1.2.2 to 1.3.0
@@ -6,3 +6,3 @@ /// <reference types="node" /> | ||
* For better security, you should choose a 256-bit keys or longer. | ||
* Once instantiated, use the encrypt() or decrypt() methods on the Cipher instance with the appropriate data. | ||
* Once instantiated, use the encrypt() or decrypt() methods on the CustomCipher instance with the appropriate data. | ||
* | ||
@@ -19,2 +19,3 @@ * @throws no key provided | ||
* @returns {Buffer} The byte array of the obfuscated result | ||
* @throws invalid string: unable to split | ||
*/ | ||
@@ -21,0 +22,0 @@ encrypt(data: string): Buffer; |
@@ -32,3 +32,3 @@ "use strict"; | ||
* For better security, you should choose a 256-bit keys or longer. | ||
* Once instantiated, use the encrypt() or decrypt() methods on the Cipher instance with the appropriate data. | ||
* Once instantiated, use the encrypt() or decrypt() methods on the CustomCipher instance with the appropriate data. | ||
* | ||
@@ -49,2 +49,3 @@ * @throws no key provided | ||
* @returns {Buffer} The byte array of the obfuscated result | ||
* @throws invalid string: unable to split | ||
*/ | ||
@@ -55,4 +56,7 @@ encrypt(data) { | ||
} | ||
// Apply the Feistel cipher | ||
// Apply the balanced Feistel cipher | ||
let parts = strings_1.split(data); | ||
if (parts.length !== 2 || parts[0].length !== parts[1].length) { | ||
throw new Error('invalid string: unable to split'); | ||
} | ||
for (let i = 0; i < this.keys.length; ++i) { // eslint-disable-line no-loops/no-loops | ||
@@ -76,3 +80,3 @@ const tmp = xor_1.xor(parts[0], this.round(parts[1], i)); | ||
} | ||
// Apply Feistel cipher | ||
// Apply the balanced Feistel cipher | ||
const parts = strings_1.split(o); | ||
@@ -79,0 +83,0 @@ let a = parts[1]; |
@@ -19,2 +19,3 @@ /// <reference types="node" /> | ||
* @returns {Buffer} The byte array of the obfuscated result. | ||
* @throws invalid string: unable to split | ||
*/ | ||
@@ -21,0 +22,0 @@ encrypt(data: string): Buffer; |
@@ -38,3 +38,3 @@ "use strict"; | ||
constructor(key, rounds) { | ||
if (key === '' || rounds === 0) { | ||
if (key === '' || rounds < 2) { | ||
throw new Error('wrong arguments'); | ||
@@ -50,2 +50,3 @@ } | ||
* @returns {Buffer} The byte array of the obfuscated result. | ||
* @throws invalid string: unable to split | ||
*/ | ||
@@ -56,4 +57,7 @@ encrypt(data) { | ||
} | ||
// Apply the Feistel cipher | ||
// Apply the balanced Feistel cipher | ||
let parts = strings_1.split(data); | ||
if (parts.length !== 2 || parts[0].length !== parts[1].length) { | ||
throw new Error('invalid string: unable to split'); | ||
} | ||
for (let i = 0; i < this.rounds; ++i) { // eslint-disable-line no-loops/no-loops | ||
@@ -77,3 +81,3 @@ const tmp = xor_1.xor(parts[0], this.round(parts[1], i)); | ||
} | ||
// Apply Feistel cipher | ||
// Apply the balanced Feistel cipher | ||
const parts = strings_1.split(o); | ||
@@ -80,0 +84,0 @@ let a = parts[1]; |
export * from './feistel'; | ||
export * from './fpe'; | ||
export * from './custom'; | ||
export * from './utils/base256'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -28,2 +28,4 @@ "use strict"; | ||
__export(require("./feistel")); | ||
__export(require("./fpe")); | ||
__export(require("./custom")); | ||
__export(require("./utils/base256")); |
/// <reference types="node" /> | ||
import { BinaryLike } from 'crypto'; | ||
export declare const Hash: (msg: BinaryLike) => Buffer; | ||
export declare const Hash: (msg: string | DataView | Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array) => Buffer; | ||
export declare type Engine = string; | ||
export declare const BLAKE2b = "blake-2b-256"; | ||
export declare const KECCAK = "keccak-256"; | ||
export declare const SHA_256 = "sha-256"; | ||
export declare const SHA_3 = "sha-3"; | ||
export declare const isAvailableEngine: (engine: string) => boolean; | ||
/** | ||
* Create a hash from the passed message using the specified algorithm | ||
* | ||
* @param {Buffer} msg - The message to hash | ||
* @param {Enging} using - The algorithm name | ||
* @returns the hashed byte array | ||
* @throws unknown hash algorithm | ||
*/ | ||
export declare const H: (msg: Buffer, using: string) => Buffer; | ||
//# sourceMappingURL=hash.d.ts.map |
@@ -23,4 +23,37 @@ "use strict"; | ||
*/ | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const crypto_1 = require("crypto"); | ||
const keccak_1 = __importDefault(require("keccak")); | ||
const sha3_1 = require("sha3"); | ||
const blake2 = require('blakejs'); // eslint-disable-line @typescript-eslint/no-var-requires | ||
exports.Hash = (msg) => crypto_1.createHash('sha256').update(msg).digest(); | ||
exports.BLAKE2b = 'blake-2b-256'; | ||
exports.KECCAK = 'keccak-256'; | ||
exports.SHA_256 = 'sha-256'; | ||
exports.SHA_3 = 'sha-3'; | ||
exports.isAvailableEngine = (engine) => engine === exports.BLAKE2b || engine === exports.KECCAK || engine === exports.SHA_256 || engine === exports.SHA_3; | ||
/** | ||
* Create a hash from the passed message using the specified algorithm | ||
* | ||
* @param {Buffer} msg - The message to hash | ||
* @param {Enging} using - The algorithm name | ||
* @returns the hashed byte array | ||
* @throws unknown hash algorithm | ||
*/ | ||
exports.H = (msg, using) => { | ||
switch (using) { | ||
case exports.BLAKE2b: | ||
return Buffer.from(blake2.blake2b(msg, '', 32), 'hex'); | ||
case exports.KECCAK: | ||
return keccak_1.default('keccak256').update(msg).digest(); | ||
case exports.SHA_256: | ||
return exports.Hash(msg); | ||
case exports.SHA_3: | ||
return new sha3_1.SHA3(256).update(msg).digest(); | ||
default: | ||
throw new Error('unknown hash algorithm'); | ||
} | ||
}; |
@@ -38,9 +38,6 @@ "use strict"; | ||
}; | ||
// Splits a string in two equal parts | ||
// Splits a string in two parts | ||
exports.split = (str) => { | ||
if (str.length % 2 != 0) { | ||
throw new Error('invalid string length: cannot be split'); | ||
} | ||
const half = str.length / 2; | ||
return [str.substr(0, half), str.substr(half)]; | ||
}; |
export declare const xor: (str1: string, str2: string) => string; | ||
export declare const NEUTRAL_BYTE: string; | ||
//# sourceMappingURL=xor.d.ts.map |
@@ -28,1 +28,2 @@ "use strict"; | ||
}; | ||
exports.NEUTRAL_BYTE = Buffer.from([0]).toString(); |
{ | ||
"name": "feistel-cipher", | ||
"version": "1.2.2", | ||
"description": "Feistel cipher implementation for (almost) format-preserving encryption", | ||
"version": "1.3.0", | ||
"description": "Feistel cipher implementation for format-preserving encryption", | ||
"main": "dist/lib/src/typescript/index.js", | ||
@@ -31,4 +31,10 @@ "types": "dist/lib/src/typescript/index.d.ts", | ||
"homepage": "https://github.com/cyrildever/feistel-cipher#readme", | ||
"peerDependencies": { | ||
"blakejs": "^1.1.0", | ||
"keccak": "^3.0.1", | ||
"sha3": "^2.1.4" | ||
}, | ||
"devDependencies": { | ||
"@types/chai": "^4.2.14", | ||
"@types/keccak": "^3.0.1", | ||
"@types/mocha": "^5.2.7", | ||
@@ -38,10 +44,11 @@ "@types/node": "^13.13.40", | ||
"@typescript-eslint/parser": "~2.30.0", | ||
"blakejs": "^1.1.0", | ||
"browserify": "16.5.0", | ||
"chai": "^4.2.0", | ||
"eslint": "~6.8.0", | ||
"eslint-plugin-immutable": "~1.0.0", | ||
"eslint-plugin-import": "~2.20.2", | ||
"eslint-plugin-no-loops": "~0.3.0", | ||
"keccak": "^3.0.1", | ||
"live-server": "^1.2.1", | ||
"mocha": "^6.2.3", | ||
"sha3": "^2.1.4", | ||
"ts-node": "^8.10.2", | ||
@@ -48,0 +55,0 @@ "typescript": "~3.8.3" |
@@ -9,4 +9,3 @@ # feistel-cipher | ||
This is a TypeScript library implementing the Feistel cipher for "almost" format-preserving encryption. | ||
"Almost" because as we use a balanced version of the implementation, we need the input string to be of even length. If that's the case, the length will be preserved, otherwise the output will be one character longer. | ||
This is a TypeScript library implementing the Feistel cipher for format-preserving encryption (FPE). | ||
@@ -37,3 +36,3 @@ ### Motivation | ||
_NB: You may also read my original white paper [here](https://github.com/cyrildever/feistel-cipher/blob/master/feistel_whitepaper.pdf)._ | ||
_NB: You may also read my original white paper [here](https://github.com/cyrildever/feistel-cipher/blob/master/feistel_whitepaper.pdf) as well as the latest one on the [full FPE version](https://github.com/cyrildever/feistel-cipher/blob/master/fpe_whitepaper.pdf)._ | ||
@@ -78,6 +77,15 @@ | ||
Finally, you might want to use the latest `FPECipher` providing true format-preserving encryption for strings: | ||
```typescript | ||
import { SHA_256 } from 'feistel-cipher' | ||
const cipher = new feistel.FPECipher(SHA_256, 'some-32-byte-long-key-to-be-safe', 128) | ||
const obfuscated = cipher.encrypt(source) | ||
assert(obfuscated.length, source.length) | ||
``` | ||
### Other implementations | ||
For those interested, I also made another implementation of this Feistel cipher in [Golang](https://github.com/cyrildever/feistel). | ||
For those interested, I also made another implementation of these Feistel ciphers in [Golang](https://github.com/cyrildever/feistel). | ||
@@ -84,0 +92,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
44562
31
749
97
3
17