Skip to content

Instantly share code, notes, and snippets.

@jhurliman
Forked from Marak/base64.js
Created September 29, 2011 06:39
Show Gist options
  • Star 58 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save jhurliman/1250118 to your computer and use it in GitHub Desktop.
Save jhurliman/1250118 to your computer and use it in GitHub Desktop.
An extremely simple implementation of base64 encoding / decoding using node.js Buffers (plus url-safe versions)
/*
* base64.js: An extremely simple implementation of base64 encoding / decoding using node.js Buffers
*
* (C) 2010, Nodejitsu Inc.
* (C) 2011, Cull TV, Inc.
*
*/
var base64 = exports;
base64.encode = function(unencoded) {
return new Buffer(unencoded || '').toString('base64');
};
base64.decode = function(encoded) {
return new Buffer(encoded || '', 'base64').toString('utf8');
};
base64.urlEncode = function(unencoded) {
var encoded = base64.encode(unencoded);
return encoded.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
};
base64.urlDecode = function(encoded) {
encoded = encoded.replace(/-/g, '+').replace(/_/g, '/');
while (encoded.length % 4)
encoded += '=';
return base64.decode(encoded);
};
@WangJi
Copy link

WangJi commented Sep 25, 2017

nice work.

@cosinekitty
Copy link

Thank you for saving me time and allowing me to have one less npm package dependency.

@mike-marcacci
Copy link

mike-marcacci commented Apr 28, 2019

These are incorrect, as using string patterns in .replace() will only replace the first instance; instead you need to use regex with the g flag:

encoded.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');

@netzmensch
Copy link

netzmensch commented Jul 15, 2019

Your line is also incorrect, as you missed the "g" for the last two replacements :)

encoded.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/g, '');

@aaronjameslang
Copy link

I don't know that it's needed for the last one as the + is greedy, but it shoudln't hurt

@EnchanterIO
Copy link

Thanks @netzmensch, encoded.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/g, ''); seems to work nicely.

@diekmrcoin
Copy link

diekmrcoin commented Mar 1, 2020

The 'g' replacing '=' is not needed, the are equals only at the end of the string. But really useful implementation :)

@metamic
Copy link

metamic commented Mar 31, 2020

Thanks @netzmensch, encoded.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/g, ''); seems to work nicely.

is work.

@alvinlao
Copy link

The decode function is also missing global "-" and "_" replacement.

Line 25 should be: encoded = encoded.replace(/-/g, '+').replace(/_/g, '/');.

@Rudiger86
Copy link

Rudiger86 commented Jul 2, 2020

Humans can I present the following for your critique.
I offer this as the replace function
'e8/51e2fa+4f/00+4609+9d/d2+9b3794c/59619====='.replace(/(\+)|(\/)|(=+$)/g,(match) => Object.create({'+': '-', '/': '_', '=': ''})[match[0]])
outputs e8_51e2fa-4f_00-4609-9d_d2-9b3794c_59619

@eteeselink
Copy link

eteeselink commented Oct 7, 2020

@Rudiger86 nice! might be faster since it only walks through the string once.

I think I got it slightly simplified to:

'e8/51e2fa+4f/00+4609+9d/d2+9b3794c/59619====='.replace(/[+/=]/g, match => 
  ({'+': '-', '/': '_', '=': ''}[match])
)

This is slightly less robust because I replace every =, but hey, in normal base64 strings =es should only ever be at the end anyway.

@roj1512
Copy link

roj1512 commented Jul 13, 2021

thanks :)

@Shaokun-X
Copy link

For ASCII only use cases:

function encode(textToEncode) {
  return btoa(textToEncode).replace(/\+/g, '~').replace(/\//g, '_').replace(/=/g, '-');
}

function decode(textToDecode: string): string {
  return atob(textToDecode.replace(/~/g, '+').replace(/_/g, '/').replace(/-/g, '='));
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment