sinosun-jwe
v0.0.3
Published
sinosun jwe tools
Downloads
1
Readme
sinosunJWE
发布到私仓
修改全局npm配置,添加访问私仓的身份令牌,避免每次发布都要登录 always-auth=true _auth="c2lub3N1bi1mcm9udC1ucG0tdXNlcjpzaW5vc3VuLWZyb250="
如果不是首次发布,需要修改组件包的版本号 方法一:直接修改package.json中的version字段 方法二:输入命令,小版本号自动加一 npm version patch
将组件包发布到私仓 1、安装依赖 npm install 2、编译 npm run pub 3、发布 npm publish
查看私仓中的组件包 地址:http://10.0.5.26:8081/#browse/browse:sinosun-front-npm-hosted 用户名:sinosun-front-npm-user 密码:sinosun-front
一般性执行流程:
jwe 加密(明文数据) -> request -> response(JWE-string) -> jwe 解密(JWE-string) -> 明文数据
其中在 **jwe 加密(明文数据)**时,会保存随机生成的 cek,在 **jwe 解密(JWE-string)**时会用到,具体可参考以下加解密案例:
import {
CompactEncrypt,
compactDecrypt,
encoder,
decoder,
KeyLike,
} from "./index";
class Jose {
static cek: KeyLike | string;
// jwe加密
static async compactEnc(): Promise<any> {
const plaintext = JSON.stringify({
url: "http://approval/getFlowId",
data: {
creator: "sda3213131dsasda",
creatorName: "江哲",
memeber: [
{ id: 1001, name: "test1" },
{ id: 1002, name: "test2" },
{ id: 1002, name: "test2" },
{ id: 1002, name: "test2" },
{ id: 1002, name: "test2" },
{ id: 1002, name: "test2" },
{ id: 1002, name: "test2" },
],
source: "browser",
},
});
const header = { alg: "SM2", enc: "G128CBC-HG128" }; //TODO 暂时没有使用压缩 zip: "DEF"
const publicKey =
"04" +
"23C0F8DCD02606990A6B5C6F363D0B19870C2197B3885026DA704D5E60C60F5799C077E5A9EBAE30822B4E458FB28A0BDE539EA49ABE44963D9C12AF8B6B8DB0";
const jweAndcek = await new CompactEncrypt(encoder.encode(plaintext))
.setProtectedHeader(header)
.encrypt(publicKey);
this.cek = jweAndcek["cek"];
console.log(`[JWE CompactEnc]: ${jweAndcek["jwe"]}`);
console.log(`[CEK]: ${this.cek}`);
}
// jwe 解密
static async compactDec(): Promise<any> {
// await this.compactEnc();
// 这里的 jwe 是请求回来的数据,这里做演示
const jwe =
"eyJhbGciOiJTTTIiLCJlbmMiOiJHMTI4Q0JDLUhHMTI4Iiwia2lkIjoidGVzdEtpZCJ9.1GbEPUoDC__uVhrMuRUNW-_lGxHWHRl06FjVJZ2u4E5PwFn3rT0cbIQtIVRD04vNV3rR5xzZi-fcT6z9-B6633CCHT7XTelaXLn4UY-lsACEzeYS6pOaLQ9oiouSAzDmVhq3llbJu5wIbl6vz0dcfcg1wtc1bKj74xIgnETFD-Q=.eyEbaD27JszMZRgQBR3FEQ==.CEpkra642HWsq8HJcquhCY6o7Cd4OjEghczioXeq9XibiM1xjHOQltUp1Ea-1-O_wEBHL-i9HfSQlmHfAWuryQ==.1en-qfZvbaU_hPFsuPbX-g==Iden";
// cek 在加密时缓存,解密时直接用,这里做演示
const cek = [
7,
96,
174,
211,
13,
160,
173,
112,
190,
90,
146,
64,
70,
155,
225,
98,
226,
207,
81,
13,
24,
181,
113,
176,
32,
12,
140,
234,
83,
16,
49,
213,
];
this.cek = new Uint8Array(cek);
/**
* 解密时确保这两者的数据格式不能有误
* jwe: string
* cek: Uint8Array
*/
const { plaintext, protectedHeader } = await compactDecrypt(jwe, this.cek);
console.log(
`[JWE CompactDec]: ${decoder.decode(plaintext)} -- ${JSON.stringify(
protectedHeader
)}`
);
}
}
Jose.compactDec();