How do I turn encoded buffer to a string

Discussion in 'JavaScript' started by ketting00, Jun 10, 2015.

  1. #1
    I got this data from a user:
    <Buffer 81 8b 4d 25 17 97 05 40 7b fb 22 05 40 f8 3f 49 73>
    Code (markup):
    I guess it was sha1 encoding.

    How do I turn it to something like this on the browser:
    'Hello World'
    Code (markup):
    I tried this it isn't work:
    
    buf = new Buffer(str.length);
    
    for (var i = 0; i < str.length ; i++) {
        buf[i] = str.charCodeAt(i);
    }
    
    console.log(buf);
    Code (markup):
    I know it needs a key. I got the key.
    Any idea how to decode it?

    Thanks,
     
    ketting00, Jun 10, 2015 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #2
    Without knowing how it was encoded that's hard to say -- if it's a hash then it's SUPPOSED to be irreversible... though that string is obviously a set of hex pairs... so if you extracted just the hex code part of the string...

    var
    	testString = '81 8b 4d 25 17 97 05 40 7b fb 22 05 40 f8 3f 49 73',
    	testArray = testString.split(' '),
    	result = '';
    	
    for (var i = 0; i < testArray.length; i++) {
    	var b = parseInt(testArray[i], 16);
    	// parse bytes here
    }
    Code (markup):
    That at least turns the hexadecimal bytes into decimal -- But without knowing the encoding or if said encoding is even supposed to be reversible? Remember, hashing algorithms are supposed to be irreversible, so short of building a rainbow table (or using an existing one)...

    Really though if it's SHA1 encoded as hexadecimal, you're going to need a proper SHA1 parser, which is NOT a simple small bit of code... and certainly NOT something one should even be screwing with client side in JS.
     
    deathshadow, Jun 10, 2015 IP
  3. ketting00

    ketting00 Well-Known Member

    Messages:
    782
    Likes Received:
    28
    Best Answers:
    3
    Trophy Points:
    128
    #3
    ketting00, Jun 10, 2015 IP