节点js中的反向散列?

时间:2017-07-25 08:05:07

标签: angularjs node.js encryption hash

我使用下面的代码来散列数据并且它正常工作。 从中获取代码 crypto website

const crypto = require('crypto');

const secret = 'abcdefg';
const hash = crypto.createHmac('sha256', secret)
                   .update('I love cupcakes')
                   .digest('hex');
console.log(hash);
// Prints:
//   c0fa1bc00531bd78ef38c628449c5102aeabd49b5dc3a2a516ea6ea959d6658e

我的问题是如何扭转这种局面? 如何再次将数据解散为普通文本?

1 个答案:

答案 0 :(得分:2)

哈希不可逆转......你需要一个密码。这是我的小秘密课程。

import crypto from 'crypto'
let Secret = new (function (){
    "use strict";

    let world_enc = "utf8"
    let secret_enc = "hex";
    let key = "some_secret_key";

    this.hide = function(payload){
        let cipher = crypto.createCipher('aes128', key);
        let hash = cipher.update(payload, world_enc, secret_enc);
        hash += cipher.final(secret_enc);
        return hash;
    };
    this.reveal = function(hash){
        let sha1 = crypto.createDecipher('aes128', key);
        let payload = sha1.update(hash, secret_enc, world_enc);
        payload += sha1.final(world_enc);
        return payload;
    }
});

export {Secret};