Base64 encode decode with key in pure javascript/typescript.
Many developers want to encode and decode base64 with privacy and don't want others to know of the many online base64 encode decode tools.
- Encode decode with key.
- Dynamic Encode decode.
- Normal Encode decode (without key).
- Generate string random (bonus).
Using npm:
$ npm install base64k
Using yarn:
$ yarn add base64k
- Initialize base64k
//esm or ts
import base64k from "base64k";
//commonjs
var base64k = require("base64k");
- Browser support
<!-- from download -->
<script src="path/to/dist/base64k.min.js"></script>
<!-- from jsdelivr -->
<script src="https://cdn.jsdelivr.net/npm/base64k@1.0.2/dist/base64k.min.js"></script>
<!-- from unpkg -->
<script src="https://unpkg.com/base64k@1.0.2/dist/base64k.min.js"></script>
- Encode and decode with key.
...
const text = "mysimpletest";
const key = "mykey";
const encode = base64k.encode(text, key);
console.log(encode);
//console.log : AAAYDBQdFQ4RHB4N
const decode = base64k.decode(encode, key);
console.log(decode);
//console.log : mysimpletest
...
- Dynamic encode and decode (generate random encode with the same string after decode.)
...
const text = "mysimpletest%&example";
const dyn_encode_1 = base64k.dynEncode(text);
const dyn_decode_1 = base64k.dynDecode(dyn_encode_1);
console.log(dyn_encode_1, " => ", dyn_decode_1);
//console.log : KSglDxc4Iw01Hzclc0AfMC4FMRYh@DQVfzHOhAz => mysimpletest%&example
const dyn_encode_2 = base64k.dynEncode(text);
const dyn_decode_2 = base64k.dynDecode(dyn_encode_2);
console.log(dyn_encode_2, " => ", dyn_decode_2);
//console.log : ITc7AQsXNgIdDz86bU4DHzsKGQYp@LNHhfgZgij => mysimpletest%&example
const dyn_encode_3 = base64k.dynEncode(text);
const dyn_decode_3 = base64k.dynDecode(dyn_encode_3);
console.log(dyn_encode_3, " => ", dyn_decode_3);
//console.log : GgkAADsgCDYAPAQEVk8zKAU+BDUS@wpsiVPdStY => mysimpletest%&example
...
- Normal encode and decode base64 (without key).
...
const text = "mysimpletest";
const encode = base64k.encode(text);
console.log(encode);
//console.log : bXlzaW1wbGV0ZXN0
const decode = base64k.decode(encode);
console.log(decode);
//console.log : mysimpletest
...
- Generate random string key (bonus).
...
//random string with combination abjad and number.
const test1 = base64k.genKey();
//console.log : LKgX4JRN4G
//random string with length 20 and combination abjad and number.
const test2 = base64k.genKey(20);
//console.log : A5upjvtz40jv71CIBNDw
//random string with length 20 and only abjad.
const test3 = base64k.genKey(20, "a");
//console.log : VsjnmnmimWYZgeohNKAa
//random string with length 20 and only number.
const test4 = base64k.genKey(20, "n");
//console.log : 36380839173686148035
...
Base64k is available for further development. made with typescript.
//clone project
git clone https://github.com/herudi/base64k.git
cd base64k
//for build
npm run build
//for test
npm run test
Contact me : herudi7@gmail.com