379

I was implementing password hashing with salt, so I generated salt as binary, hashed the password, base64 encoded the password and salt then stored them into database.

Now when I am checking password, I am supposed to decode the salt back into binary data, use it to hash the supplied password, base64 encode the result and check if the result matches the one in database.

The problem is, I cannot find a method to decode the salt back into binary data. I encoded them using the Buffer.toString method but there doesn't seem to be reverse function.

Xavier_Ex
  • 8,432
  • 11
  • 39
  • 55

1 Answers1

804

As of Node.js v6.0.0 using the constructor method has been deprecated and the following method should instead be used to construct a new buffer from a base64 encoded string:

var b64string = /* whatever */;
var buf = Buffer.from(b64string, 'base64'); // Ta-da

For Node.js v5.11.1 and below

Construct a new Buffer and pass 'base64' as the second argument:

var b64string = /* whatever */;
var buf = new Buffer(b64string, 'base64'); // Ta-da

If you want to be clean, you can check whether from exists :

if (typeof Buffer.from === "function") {
    // Node 5.10+
    buf = Buffer.from(b64string, 'base64'); // Ta-da
} else {
    // older Node versions, now deprecated
    buf = new Buffer(b64string, 'base64'); // Ta-da
}
аlex
  • 5,426
  • 1
  • 29
  • 38
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • Thanks! Unfortunately their documentation isn't clear on this function ... (http://nodejs.org/docs/v0.4.8/api/buffers.html#new_Buffer) – Xavier_Ex Jan 29 '13 at 15:19
  • 2
    It's not _that_ bad, the 3rd constructor is right there. – Matt Ball Jan 29 '13 at 15:34
  • 1
    So how do we know if it returns invalid? Where's the documentation? – Oliver Dixon Aug 30 '15 at 11:23
  • 43
    And for those searching the reverse method - `( new Buffer( str ) ).toString( "base64" )`. – Nikolay Tsenkov Sep 05 '15 at 18:33
  • 14
    This method has been deprecated now, the correct way is now: `var buf = Buffer.from(b64string, 'base64');` as noted here: https://nodejs.org/api/buffer.html#buffer_buffers_and_character_encodings – Kristoffer Dorph May 08 '16 at 20:48
  • @KristofferDorph thanks for the updated info. Would you mind suggesting an edit to this answer, please? – Matt Ball May 09 '16 at 18:43
  • 1
    The part that @nathanhleung mentioned should be part of the answer! – Kunok Nov 27 '16 at 12:54
  • 4
    FYI, this code does not appear to work on older Node versions. Buffer.from is still a function on node 4.3.6, but 'TypeError: base64 is not a function' – Doug Dec 21 '16 at 21:59
  • @Doug, it was raising this unclear error for me too, had to upgrade node and it started working. – Evgenia Karunus Jan 30 '17 at 19:51
  • 19
    If `toString('ascii')` isn't working for you, try this instead: `Buffer.from(string, 'base64').toString('utf-8')` – joeytwiddle Apr 23 '18 at 08:37
  • Use decode string from base64 to string : let text = Buffer.from(imgurl, "base64").toString("utf-8"); – dinuka saminda Nov 02 '19 at 14:29
  • 1
    You got it wrong. It is `const buf = Buffer.from('abc', 'utf-8').toString('base64');` – Samuel O Feb 10 '21 at 19:36
  • Just want to say that using Buffer that way in Node seems to be deprecated. A prefered way to do this would be: `const binary = Buffer.from(base64String, 'base64');` – Yves Gurcan Jul 07 '21 at 16:57
  • To add some to the reverse method, you can decode them by calling atob(). ```const a = new Buffer(str).toString('base64'); const decodedData = atob(a)``` – Dave Lee Jan 12 '23 at 16:43