-
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.
- Loading branch information
Showing
6 changed files
with
93 additions
and
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,55 @@ | ||
const crypto = require('crypto'); | ||
|
||
const IV_LENGTH = 16; | ||
const CRYPT_METHOD = 'aes-128-cbc'; | ||
const SECRET_LENGTH = 16; // 128 bit or 16 characters | ||
const CRYPT_METHOD = 'aes-256-gcm'; | ||
const SECRET_LENGTH = 32; // 256 bit or 32 characters | ||
|
||
function RDCrypto(secret) { | ||
if (secret.length !== SECRET_LENGTH) { | ||
throw new Error(`Secret length should be exactly ${IV_LENGTH} characters long`); | ||
function RDCrypto(secret, salt) { | ||
if (secret.length < IV_LENGTH) { | ||
throw new Error(`Secret length should be greater than ${IV_LENGTH} characters`); | ||
} | ||
|
||
this.cryptoMarker = '-CRYPT-'; | ||
this.secret = secret; | ||
this.cryptoMarker = '-CRYPT-V2-'; | ||
this.secret = crypto.pbkdf2Sync(secret, salt, 100, SECRET_LENGTH, 'sha3-256'); | ||
} | ||
|
||
RDCrypto.prototype.encrypt = function (value) { | ||
const iv = crypto.randomBytes(IV_LENGTH); | ||
const cipher = crypto.createCipheriv(CRYPT_METHOD, Buffer.from(this.secret), iv); | ||
const cipher = crypto.createCipheriv(CRYPT_METHOD, this.secret, iv); | ||
let encrypted = cipher.update(value); | ||
encrypted = Buffer.concat([encrypted, cipher.final()]); | ||
const tag = cipher.getAuthTag(); | ||
|
||
return `${this.cryptoMarker}${iv.toString('hex')}${encrypted.toString('hex')}`; | ||
return `${this.cryptoMarker}${iv.toString('hex')}${encrypted.toString('hex')}${tag.toString("hex")}`; | ||
}; | ||
|
||
RDCrypto.prototype.decrypt = function (value) { | ||
if (0 !== value.indexOf(this.cryptoMarker)) { | ||
return value; | ||
} | ||
|
||
if (value.length < SECRET_LENGTH + IV_LENGTH * 2) { | ||
return value; | ||
} | ||
|
||
const originalValue = value; | ||
value = value.substr(this.cryptoMarker.length); | ||
|
||
try { | ||
const iv = Buffer.from(value.substring(0, IV_LENGTH * 2), "hex"); | ||
const encrypted = Buffer.from(value.substring(IV_LENGTH * 2), "hex"); | ||
const encrypted = Buffer.from(value.substring(IV_LENGTH * 2, value.length - SECRET_LENGTH), "hex"); | ||
const tag = Buffer.from(value.slice(-SECRET_LENGTH), "hex"); | ||
|
||
const decipher = crypto.createDecipheriv(CRYPT_METHOD, Buffer.from(this.secret), iv); | ||
let decrypted = decipher.update(encrypted); | ||
const decipher = crypto.createDecipheriv(CRYPT_METHOD, this.secret, iv); | ||
decipher.setAuthTag(tag); | ||
const decrypted = decipher.update(encrypted); | ||
|
||
decrypted = Buffer.concat([decrypted, decipher.final()]); | ||
return decrypted.toString(); | ||
return Buffer.concat([decrypted, decipher.final()]).toString(); | ||
} catch (err) { | ||
return originalValue; | ||
} | ||
}; | ||
|
||
module.exports = function (secret) { | ||
return new RDCrypto(secret); | ||
module.exports = function (secret, salt) { | ||
return new RDCrypto(secret, salt); | ||
}; |
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