@4us-dev/crypto 中文文档教程

发布于 3年前 浏览 30 项目主页 更新于 3年前

4us-dev crypto

质量门状态覆盖范围代码味道漏洞可维护性评级安全评级

构建和测试 npm-packageissues依赖项dev-dependencies下载大小 license

使用 npm

npm i @4us-dev/crypto

Introduction

@4us-dev/crypto 是一个帮助开发人员进行加密和编码的库。

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';

默认编码为 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 verifiercode 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

Quality Gate StatusCoverageCode SmellsVulnerabilitiesMaintainability RatingSecurity Rating

Build and Tests npm-packageissuesdependenciesdev-dependenciesdownloadssize license

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
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文