1.0.0 • Published 4 years ago

base-64 v1.0.0

Weekly downloads
594,391
License
MIT
Repository
github
Last release
4 years ago

base64 Build status Code coverage status

base64 is a robust base64 encoder/decoder that is fully compatible with atob() and btoa(), written in JavaScript. The base64-encoding and -decoding algorithms it uses are fully RFC 4648 compliant.

Installation

Via npm:

npm install base-64

In a browser:

<script src="base64.js"></script>

In Narwhal, Node.js, and RingoJS:

var base64 = require('base-64');

In Rhino:

load('base64.js');

Using an AMD loader like RequireJS:

require(
  {
    'paths': {
      'base64': 'path/to/base64'
    }
  },
  ['base64'],
  function(base64) {
    console.log(base64);
  }
);

API

base64.version

A string representing the semantic version number.

base64.encode(input)

This function takes a byte string (the input parameter) and encodes it according to base64. The input data must be in the form of a string containing only characters in the range from U+0000 to U+00FF, each representing a binary byte with values 0x00 to 0xFF. The base64.encode() function is designed to be fully compatible with btoa() as described in the HTML Standard.

var encodedData = base64.encode(input);

To base64-encode any Unicode string, encode it as UTF-8 first:

var base64 = require('base-64');
var utf8 = require('utf8');

var text = 'foo © bar 𝌆 baz';
var bytes = utf8.encode(text);
var encoded = base64.encode(bytes);
console.log(encoded);
// → 'Zm9vIMKpIGJhciDwnYyGIGJheg=='

base64.decode(input)

This function takes a base64-encoded string (the input parameter) and decodes it. The return value is in the form of a string containing only characters in the range from U+0000 to U+00FF, each representing a binary byte with values 0x00 to 0xFF. The base64.decode() function is designed to be fully compatible with atob() as described in the HTML Standard.

var decodedData = base64.decode(encodedData);

To base64-decode UTF-8-encoded data back into a Unicode string, UTF-8-decode it after base64-decoding it:

var encoded = 'Zm9vIMKpIGJhciDwnYyGIGJheg==';
var bytes = base64.decode(encoded);
var text = utf8.decode(bytes);
console.log(text);
// → 'foo © bar 𝌆 baz'

Support

base64 is designed to work in at least Node.js v0.10.0, Narwhal 0.3.2, RingoJS 0.8-0.9, PhantomJS 1.9.0, Rhino 1.7RC4, as well as old and modern versions of Chrome, Firefox, Safari, Opera, and Internet Explorer.

Unit tests & code coverage

After cloning this repository, run npm install to install the dependencies needed for development and testing. You may want to install Istanbul globally using npm install istanbul -g.

Once that’s done, you can run the unit tests in Node using npm test or node tests/tests.js. To run the tests in Rhino, Ringo, Narwhal, and web browsers as well, use grunt test.

To generate the code coverage report, use grunt cover.

Author

twitter/mathias
Mathias Bynens

License

base64 is available under the MIT license.

@pubngo-stack/account-api-mobile@courselit/api@courselit/app@borderfreefinancial/revo-consumer@borderfreefinancial/revo-core@borderfreefinancial/revo-core-api@borderfreefinancial/revo-web-video-test@borderfreefinancial/revoconsumermobileapp-test@borderfreefinancial/revoshowmobileui-test@borderfreefinancial/revoshowui-testdoctorstrange-updater@atomist/automation-client@jthftc/first-npmseatent-rn-fetch-blob@ciro-maciel/gulp-git-push@yllet/transport-fetchnodejs-k8s-client.gitreact-native-goipfsreact-native-meteor-forkkms-crypto-conf@noname.team/crypto-libtapfood-http-infrastructureattributes-v4honeywell-apihoneywellhome-apiodcj@mubeen_ahmed/epubjs-rnapi_ordersgq-tour-transformer@seongjoojin/epubjs-rngoldmine-clientftc-testinforapipackageajitkamath-shared-libxlx-ossuploadi-invest-wealth@luxrobo/package-generatorjuno-hashcard-moduleferring_cmi_api@jessitron/automation-clientsample-chat-notification@luxrobo/modi-io@luxrobo/module-manager@luxrobo/packet-generatorreact-native-fs-refis360movilpbfintechfzegenie-rn-fetch-blobapi-projectairasia1222@dlenroc/appium-playwright-drivermacwk-sdk-jswisekaa03-mailgun.jswykop-sdk1-nodefetch-github-api@bleco/authentication-servicevisdi-spotify-api-tsdykijs-extendingssourcecred-for-samudai@bucan-labs/sales-sdk@nulliel/aws-lambda-envbitnucleusedume-commonrn-expo-firebase-authreact-native-chatkittywilscannerghostwriter-serviceskill-f3@fclebinho/react-native-keycloak-plugin@xavo/kubernetes-client-nodemetakey-lib7boss-store-users-api@zerobytellc/rn-fetch-blob@microloop/ms-authentication@lost-worlds/sdkreact-native-agora-rtc-ngtesting-asd1234socketcluster-client-edgesocketcluster-es6-clienttoccojsisadb-passwordsubiportalreact-away@everything-registry/sub-chunk-1215gamification-react-testwebfile-packerwebfile-packer-clientreact-application-server@_koii/k2-nodezero-wolf-fs@fuknando/easy-crc@fungible-systems/node-libs-expo@frankieone/one-sdk@fyresite/digisigner-js@gameroom/emporium@gameroom/gameroom-kit@gameroom/kit@fridaygame/client@functionland/rn-borg@functionland/rn-fula
1.0.0

4 years ago

0.1.0

10 years ago