@4us-dev/crypto
v0.1.1
Published
Utility classes to assist developers during software development
Downloads
89
Maintainers
Readme
4us-dev crypto
Install with npm
npm i @4us-dev/crypto
Introduction
@4us-dev/crypto
is a lib to help developers with encryption and encoding.
Usage
Base64
import { base64 } from '@4us-dev/crypto';
encode
base64.encode('teste+encode+');
// output
// dGVzdGUrZW5jb2RlKw==
decode
base64.decode('dGVzdGUrZW5jb2RlKw==');
// output
// teste+encode+
Base64Url
import { base64Url } from '@4us-dev/crypto';
encode
base64Url.encode('teste+encode+');
// output
// dGVzdGUrZW5jb2RlKw
decode
base64Url.decode('dGVzdGUrZW5jb2RlKw');
// output
// teste+encode+
sha256
import { sha256 } from '@4us-dev/crypto';
default encoding is hex
sha256('4us dev');
// output
// 9c8813f0ca966480ebd17668f7850c85930e9a5e2425a71d9b1125c9026edf3d
defining output encode
sha256('4us dev', 'base64');
// output
// nIgT8MqWZIDr0XZo94UMhZMOml4kJacdmxElyQJu3z0=
sha256Async
import { sha256Async } from '@4us-dev/crypto';
const output = await sha256Async('4us dev');
// or
sha256Async('4us dev').then((output) => console.log(output));
// output
// 9c8813f0ca966480ebd17668f7850c85930e9a5e2425a71d9b1125c9026edf3d
PKCE
Generate code verifier
and code challenge
for OAuth with PKCE.
import { pkce } from '@4us-dev/crypto';
codeVerifierGenerator
const codeVerifier = pkce.codeVerifierGenerator();
// output example
// FVMq~J2BfdXm2cEOEtlKQTGNRjYQ8VYA6OETFrW3A915~8dbktXkSwtVbZnUUStkApwZjcIWCYV.OiITpYNXpuWGBEM1.oFxT7ZizzoQ0OE81ZAOIU1JCwXMxmCQtyL2
codeChallengeGenerator
// this works only with nodejs
const output = pkce.codeChallengeGenerator(codeVerifier);
// output example
// Ae8fgMraIhwQDPladfacB-s6Oh4Hzs34SXLQmiyci98
codeChallengeGeneratorAsync
// this works with browser and node
const output = await pkce.codeChallengeGeneratorAsync(codeVerifier);
// output example
// Ae8fgMraIhwQDPladfacB-s6Oh4Hzs34SXLQmiyci98