-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from goodwid/master
- Loading branch information
Showing
6 changed files
with
1,086 additions
and
77 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const crypto = require('crypto') | ||
|
||
const algorithm = 'aes-256-ctr' | ||
const inputEncoding = 'utf8' | ||
const outputEncoding = 'hex' | ||
|
||
function encode (text, key) { | ||
const iv = crypto.randomBytes(8).toString('hex') | ||
const cipher = crypto.createCipheriv(algorithm, key, iv) | ||
const crypted = cipher.update(text, inputEncoding, outputEncoding) + cipher.final(outputEncoding) | ||
return `${iv.toString('hex')}:${crypted.toString()}` | ||
} | ||
|
||
function decode (text, key) { | ||
const [ivstring, encrypted] = text.split(':') | ||
const decipher = crypto.createDecipheriv(algorithm, key, ivstring) | ||
return decipher.update(encrypted, outputEncoding, inputEncoding) + decipher.final(inputEncoding) | ||
} | ||
|
||
function legacyDecode (text, password) { | ||
const decipher = crypto.createDecipher('aes128', password) | ||
return decipher.update(String(text), outputEncoding, inputEncoding) + decipher.final(inputEncoding) | ||
} | ||
|
||
module.exports = { | ||
encode, | ||
decode, | ||
legacyDecode, | ||
}; |
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,5 @@ | ||
module.exports = (text, padding) => { | ||
if (text.length === 32) return text | ||
if (text.length < 32) return text.padEnd(32, padding); | ||
if (text.length > 32) return text.slice(0, 32); | ||
}; |
Oops, something went wrong.