Skip to content

Instantly share code, notes, and snippets.

@tianjianchn
Forked from jhurliman/base64.js
Created April 29, 2016 15:39
Show Gist options
  • Save tianjianchn/beb4b12230808ac872172baf3c6e72a3 to your computer and use it in GitHub Desktop.
Save tianjianchn/beb4b12230808ac872172baf3c6e72a3 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);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment