2017-07-25 71 views
0

我正在使用下面的代码散列数据,它工作正常。 拿着从 crypto website节点js中的反向哈希?

const crypto = require('crypto'); 

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

我的问题是代码如何扭转呢? 如何将数据再次散列为正常文本?

+3

你没有。这就是散列的意思。您可能正在寻找* encryption *。 – Robert

+0

[哈希和加密算法之间的根本区别]的可能副本(https://stackoverflow.com/questions/4948322/fundamental-difference-between-hashing-and-encryption-algorithms) –

回答

0

哈希不能颠倒......你需要一个密码。这是我的小而简单的秘密课程。

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};