Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get hash from strings, like hashids

Using the package hashids, I can obtain hashes (with encode and decode) from numbers.

    var Hashids = require("hashids"),
        hashids = new Hashids("this is my salt", 8);
    
    var id = hashids.encode(1);

Is there a similar package to obtain hashes from strings? (with encode and decode)

like image 668
JuanPablo Avatar asked Nov 06 '14 14:11

JuanPablo


1 Answers

var Hashids = require("hashids");
var hashids = new Hashids("this is my salt");

var hex = Buffer.from('Hello World', 'utf8').toString('hex');
console.log (hex); // '48656c6c6f20576f726c64'

var encoded = hashids.encodeHex(hex);
console.log (encoded); // 'rZ4pPgYxegCarB3eXbg'

var decodedHex = hashids.decodeHex('rZ4pPgYxegCarB3eXbg');
console.log (decodedHex); // '48656c6c6f20576f726c64'

var string = Buffer.from('48656c6c6f20576f726c64', 'hex').toString('utf8');
console.log (string); // 'Hello World'
like image 50
Roman Rhrn Nesterov Avatar answered Oct 16 '22 07:10

Roman Rhrn Nesterov