węzeł.js hash string?

Mam ciąg, który chcę hashować. Jak najłatwiej wygenerować hash w węźle.js?

Hash służy do wersjonowania, nie do zabezpieczenia.

 259
Author: Francisco Couzo, 2011-05-04

8 answers

Na wykresie tygodniowymcreateHash (algorithm)

var filename = process.argv[2];
var crypto = require('crypto');
var fs = require('fs');

var md5sum = crypto.createHash('md5');

var s = fs.ReadStream(filename);
s.on('data', function(d) {
  md5sum.update(d);
});

s.on('end', function() {
  var d = md5sum.digest('hex');
  console.log(d + '  ' + filename);
});
 183
Author: timbooo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-05-22 15:30:49

Jeśli chcesz md5 hash prosty ciąg znalazłem to działa dla mnie.

var crypto = require('crypto');
var name = 'braitsch';
var hash = crypto.createHash('md5').update(name).digest('hex');
console.log(hash); // 9b74c9897bac770ffc029102a200c5de
 564
Author: braitsch,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-02-15 09:00:31

API modułu kryptograficznego węzła jest nadal niestabilne.

Od wersji 4.0.0 macierzysty moduł kryptograficzny nie jest już niestabilny. Z oficjalnej dokumentacji :

Crypto

Stabilność: 2-Stabilna

API okazało się zadowalające. Zgodność z ekosystemem npm jest to wysoki priorytet i nie zostanie złamany, chyba że jest to absolutnie konieczne.

Więc należy uważać za bezpieczne korzystanie z natywnego implementacja, bez zewnętrznych zależności.

Dla odniesienia, Moduły wymienione poniżej zostały zasugerowane jako alternatywne rozwiązania, gdy moduł kryptograficzny był nadal niestabilny.


Możesz również użyć jednego z modułów sha1 lub md5 , które wykonują to zadanie.

$ npm install sha1

A następnie

var sha1 = require('sha1');

var hash = sha1("my message");

console.log(hash); // 104ab42f1193c336aa2cf08a2c946d5c6fd0fcdb

Lub

$ npm install md5

A następnie

var md5 = require('md5');

var hash = md5("my message");

console.log(hash); // 8ba6c19dc1def5702ff5acbf2aeea5aa

(MD5 jest niepewny, ale często używany przez usługi takie jak Gravatar.)

API tych modułów nie zmieni się!

 74
Author: pvorb,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-24 21:52:08
sha256("string or binary");

Doświadczyłem problemu z inną odpowiedzią. Radzę ci ustawić argument kodowania na binary, Aby używać ciągu bajtów i zapobiegać różnym hashom między Javascript (NodeJS) a innymi językami / usługami, takimi jak Python, PHP, Github...

Jeśli nie użyjesz tego kodu, możesz uzyskać inny hash między NodeJS i Pythonem...

Jak uzyskać ten sam hash co Python, PHP, Perl, Github (i zapobiec problemowi):]}

NodeJS hashuje reprezentację UTF-8 sznurek. Inne języki (jak Python, PHP czy PERL...) hashują łańcuch bajtów.

Możemy dodać binarny argument, aby użyć ciągu bajtów.

Kod:

const crypto = require("crypto");

function sha256(data) {
    return crypto.createHash("sha256").update(data, "binary").digest("base64");
    //                                               ------  binary: hash the byte string
}

sha256("string or binary");

Dokumentacja:

  • krypto.createHash (algorithm [, options]): algorytm jest zależny od dostępnych algorytmów obsługiwanych przez wersję OpenSSL na platformie.
  • hash.digest ([encoding]): kodowanie może być "hex", "latin1" lub "base64" (baza 64 jest krótsza).

Możesz uzyskać problem z: sha256("\XAC"), "\xd1", "\XB9", "\xe2", "\xbb", "\x93", itd...

  • Inne języki (jak PHP, Python, Perl...) i moje rozwiązanie z .update(data, "binary"):

    sha1("\xac") //39527c59247a39d18ad48b9947ea738396a3bc47
    
  • Domyślnie Nodejs (bez binarnych):

    sha1("\xac") //f50eb35d94f1d75480496e54f4b4a472a9148752
    
 15
Author: Hors Sujet,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-06-30 09:33:34

Moduł crypto bardzo to ułatwia.

Konfiguracja:

const crypto = require('crypto');

const sha256 = x => crypto.createHash('sha256').update(x, 'utf8').digest('hex');

Użycie:

sha256('Hello, world. ');
 9
Author: sdgfsdh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-04-05 13:37:09

Tutaj możesz porównać wszystkie wspierane skróty na twoim sprzęcie, obsługiwane przez twoją wersję węzła.js. Niektóre są kryptograficzne, a niektóre są tylko dla sumy kontrolnej. Jego obliczanie "Hello World" milion razy dla każdego algorytmu. Może to potrwać około 1-15 sekund dla każdego algorytmu (testowanego na standardowym silniku Google Computing z Node.js 4.2.2).

for(var i1=0;i1<crypto.getHashes().length;i1++){
  var Algh=crypto.getHashes()[i1];
  console.time(Algh);
  for(var i2=0;i2<1000000;i2++){
    crypto.createHash(Algh).update("Hello World").digest("hex");
  }
  console.timeEnd(Algh);  
}

Wynik:
DSA: 1992ms
DSA-SHA: 1960ms
DSA-SHA1: 2062ms
DSA-SHA1-old: 2124ms
RSA-MD4: 1893ms
RSA-MD5: 1982ms
RSA-MDC2: 2797ms
RSA-RIPEMD160: 2101ms
RSA-SHA: 1948ms
RSA-SHA1: 1908ms
RSA-SHA1-2: 2042ms
RSA-SHA224: 2176ms
RSA-SHA256: 2158ms
RSA-SHA384: 2290ms
RSA-SHA512: 2357ms
dsaencryption: 1936ms
dsaWithSHA: 1910ms
dsaWithSHA1: 1926ms
dss1: 1928ms
ecdsa-with-SHA1: 1880ms
md4: 1833ms
md4WithRSAEncryption: 1925ms
md5: 1863ms
md5WithRSAEncryption: 1923ms
mdc2: 2729ms
mdc2WithRSA: 2890ms
ripemd: 2101ms
ripemd160: 2153ms
ripemd160WithRSA: 2210ms
rmd160: 2146ms
sha: 1929ms
sha1: 1880ms
sha1WithRSAEncryption: 1957ms
sha224: 2121ms
sha224WithRSAEncryption: 2290ms
sha256: 2134ms
sha256WithRSAEncryption: 2190ms
sha384: 2181ms
sha384WithRSAEncryption: 2343ms
sha512: 2371ms
sha512WithRSAEncryption: 2434ms
shawithrsaencryption: 1966ms
ssl2-md5: 1853ms
ssl3-md5: 1868ms
ssl3-sha1: 1971ms
Whirlpool: 2578ms

 6
Author: user3077458,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-11-09 22:08:34

Rozważając myśli z http://www.thoughtcrime.org/blog/the-cryptographic-doom-principle / (w skrócie: najpierw Szyfruj, a następnie Uwierzytelnij. Następnie najpierw zweryfikuj, a następnie odszyfruj) Zaimplementowałem następujące rozwiązanie w node.js:

function encrypt(text,password){
  var cipher = crypto.createCipher(algorithm,password)
  var crypted = cipher.update(text,'utf8','hex')
  crypted += cipher.final('hex');
  return crypted;
}

function decrypt(text,password){
  var decipher = crypto.createDecipher(algorithm,password)
  var dec = decipher.update(text,'hex','utf8')
  dec += decipher.final('utf8');
  return dec;
}

function hashText(text){
    var hash = crypto.createHash('md5').update(text).digest("hex");
    //console.log(hash); 
    return hash;
}

function encryptThenAuthenticate(plainText,pw)
{
    var encryptedText = encrypt(plainText,pw);
    var hash = hashText(encryptedText);
    return encryptedText+"$"+hash;
}
function VerifyThenDecrypt(encryptedAndAuthenticatedText,pw)
{
    var encryptedAndHashArray = encryptedAndAuthenticatedText.split("$");
    var encrypted = encryptedAndHashArray[0];
    var hash = encryptedAndHashArray[1];
    var hash2Compare = hashText(encrypted);
    if (hash === hash2Compare)
    {
        return decrypt(encrypted,pw); 
    }
}

Można go przetestować za pomocą:

var doom = encryptThenAuthenticate("The encrypted text",user.cryptoPassword);
console.log(VerifyThenDecrypt(doom,user.cryptoPassword));

Mam nadzieję, że to pomoże: -)

 1
Author: batomaeus,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-04-18 09:59:34

Używam blueimp-md5 , który jest "kompatybilny ze środowiskami po stronie serwera, takimi jak Node.js, module loaders jak RequireJS, Browserify lub webpack i wszystkie przeglądarki internetowe."

Użyj go tak:

var md5 = require("blueimp-md5");

var myHashedString = createHash('GreensterRox');

createHash(myString){
    return md5(myString);
}

Jeśli przekazujesz wartości hashowane na otwartej przestrzeni, zawsze dobrze jest je solić, aby trudniej było je odtworzyć:

createHash(myString){
    var salt = 'HnasBzbxH9';
    return md5(myString+salt);
}
 1
Author: GreensterRox,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-10-13 11:05:09