Created
September 2, 2020 20:32
-
-
Save sloev/25b4627cbf67f3b31061736e70359b96 to your computer and use it in GitHub Desktop.
encrypt and base2048 encode json bidirectional
This file contains hidden or 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
| const { encode, decode } = require('base2048'); | |
| const jsonCipher = require('json-cipher') | |
| createCrypto = (secret, algorithm = 'aes-256-cbc') => { | |
| const { cipher, decipher, hmac } = jsonCipher(secret, algorithm) | |
| return { | |
| stringify: object => encode(cipher(object)), | |
| parse: string => decipher(decode(string)), | |
| cipher, | |
| decipher, | |
| hmac, | |
| hash: hmac, | |
| } | |
| } | |
| original = {p:"MEGA LOOOOOOONG PASSWORD", h:"QmTrs4KkyUXvjEhPKYnUhu1vUoy9e9yPpVytqnLsovNY6Y"} | |
| console.log('original', original) | |
| crypto = createCrypto("REALLY TOP SECRET STUFF!!") | |
| str = crypto.stringify(original) | |
| console.log('encrypted', str) | |
| output = crypto.parse(str) | |
| console.log('decrypted', output) | |
| otherCrypto = createCrypto("REALLY TOP SECRET STUFFz!!") | |
| output = otherCrypto.parse(str) | |
| console.log(output) | |
| // original { p: 'MEGA LOOOOOOONG PASSWORD', | |
| // h: 'QmTrs4KkyUXvjEhPKYnUhu1vUoy9e9yPpVytqnLsovNY6Y' } | |
| // encrypted শ௫ƃ߀יࠊດഖזඹٿHਪ୦ǀƙ༡Ð૧ฉȿvߎڞϼޣདࢫຫࠄযઆဥహဍߥઓخגحຮҰލۏಅݛ૫Ьʩڹǃdཎ౫ڻΟԺȠޞࠎѲϘઽԽ௩အεൻණನ | |
| // decrypted { p: 'MEGA LOOOOOOONG PASSWORD', | |
| // h: 'QmTrs4KkyUXvjEhPKYnUhu1vUoy9e9yPpVytqnLsovNY6Y' } | |
| // crypto.js:183 | |
| // var ret = this._handle.final(); | |
| // ^ | |
| // Error: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt | |
| // at Decipher.final (crypto.js:183:26) | |
| // at decipher (/Users/jgv/Documents/sheik/node_modules/json-cipher/index.js:17:72) | |
| // at Object.parse (/Users/jgv/Documents/sheik/index2.js:8:22) | |
| // at Object.<anonymous> (/Users/jgv/Documents/sheik/index2.js:25:22) | |
| // at Module._compile (module.js:653:30) | |
| // at Object.Module._extensions..js (module.js:664:10) | |
| // at Module.load (module.js:566:32) | |
| // at tryModuleLoad (module.js:506:12) | |
| // at Function.Module._load (module.js:498:3) | |
| // at Function.Module.runMain (module.js:694:10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment