@4us-dev/crypto 中文文档教程
4us-dev crypto
使用 npm
npm i @4us-dev/crypto
Introduction
@4us-dev/crypto
是一个帮助开发人员进行加密和编码的库。
Usage
- Base64
- encode
- decode
- Base64Url
- encode
- decode
- sha256
- Defining output encode
- sha256Async
- PKCE
- codeVerifierGenerator
- codeChallengeGenerator
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';
默认编码为 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
使用 PKCE 为 OAuth 生成 code verifier
和 code challenge
。
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
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
- encode
- decode
- Base64Url
- encode
- decode
- sha256
- Defining output encode
- sha256Async
- PKCE
- codeVerifierGenerator
- codeChallengeGenerator
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
更多