2017-07-25 70 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};