Skip to content

Commit 8719348

Browse files
committed
配置调整,删除无用代码,类型新增
1 parent bb876f8 commit 8719348

File tree

7 files changed

+116
-65
lines changed

7 files changed

+116
-65
lines changed

Diff for: READEME.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# web端和deno的原生加解密工具
2+
3+
## AESCBC
4+
5+
对称密钥算法.用于加解密,解析arraybuffer为密钥对象
6+
7+
## HMAC
8+
9+
对称密钥签名和验证,解析密钥为密钥对象,导出密钥为arraybuffer
10+
11+
## RSAOAEP
12+
13+
非对称密钥算法,用于加解密数据,解析arraybuffer公钥为密钥对象,导出公钥为arraybuffer,生成密钥对
14+
15+
## RSAPASS
16+
17+
非对称密钥算法,用于签名和验证,解析arraybuffer公钥为密钥对象,导出公钥为arraybuffer,生成密钥对
18+
19+
## RSAOAEP-PKCS1-v1_5
20+
21+
非对称密钥算法,用于验证和签名,解析arraybuffer公钥为密钥对象,导出公钥为arraybuffer,生成密钥对

Diff for: deno.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
{
2-
"name": "",
2+
"name": "@advanced/crypto",
3+
"version": "0.0.1",
4+
"exports": "./mod.ts",
35
"tasks": {
4-
"dev": "deno run --watch main.ts"
6+
"test": "deno test"
57
},
6-
"test":{
8+
"test": {
79
"include": ["test/**/*test.ts"]
810
}
911
}

Diff for: mod.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export * from './src/aes.ts'
2-
export * from './src/hmac.ts'
3-
export * from './src/rsa.ts'
1+
export * from "./src/aes.ts";
2+
export * from "./src/hmac.ts";
3+
export * from "./src/rsa.ts";

Diff for: src/aes.ts

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/**
22
* @description aes cbc hash-128
3-
*
43
*/
54
export class AESCBC {
65
#key?: ArrayBuffer | Uint8Array;
@@ -12,7 +11,7 @@ export class AESCBC {
1211
this.#key = key;
1312
}
1413
}
15-
static parseKey(serverKey: ArrayBuffer) {
14+
static parseKey(serverKey: ArrayBuffer):Promise<CryptoKey> {
1615
return crypto.subtle.importKey(
1716
"raw",
1817
serverKey,
@@ -25,13 +24,9 @@ export class AESCBC {
2524
if (this.#cryptoKey && !force) {
2625
return;
2726
}
28-
if (!this.#key) {
29-
console.error("意外的初始化:无效的密钥");
30-
return;
31-
}
3227
this.#cryptoKey = await crypto.subtle.importKey(
3328
"raw",
34-
this.#key,
29+
this.#key!,
3530
{ name: "AES-CBC", length: 128 },
3631
true,
3732
["encrypt", "decrypt"],
@@ -40,7 +35,7 @@ export class AESCBC {
4035
async encrypt(
4136
plaintext: ArrayBuffer | Uint8Array,
4237
iv: ArrayBuffer | Uint8Array,
43-
) {
38+
):Promise<ArrayBuffer> {
4439
if (!this.#cryptoKey) {
4540
await this.initCryptoKey();
4641
}
@@ -54,7 +49,7 @@ export class AESCBC {
5449
async decrypt(
5550
ciphertext: ArrayBuffer | Uint8Array,
5651
iv: ArrayBuffer | Uint8Array,
57-
) {
52+
):Promise<ArrayBuffer> {
5853
if (!this.#cryptoKey) {
5954
await this.initCryptoKey();
6055
}

Diff for: src/hmac.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ export class HMAC {
2727
["sign", "verify"],
2828
);
2929
}
30-
exportKey(key: CryptoKey) {
30+
public exportKey(key: CryptoKey):Promise<ArrayBuffer> {
3131
return crypto.subtle.exportKey("raw", key);
3232
}
33-
static async GenerateKey() {
33+
static async GenerateKey():Promise<CryptoKey> {
3434
const key = await crypto.subtle.generateKey(
3535
{
3636
name: "HMAC",
@@ -41,7 +41,7 @@ export class HMAC {
4141
);
4242
return key;
4343
}
44-
static parseKey(serverKey: ArrayBuffer) {
44+
static parseKey(serverKey: ArrayBuffer):Promise<CryptoKey> {
4545
return crypto.subtle.importKey(
4646
"raw",
4747
serverKey,
@@ -53,15 +53,15 @@ export class HMAC {
5353
["sign", "verify"],
5454
);
5555
}
56-
static async sign(key: CryptoKey, data: ArrayBuffer) {
56+
static async sign(key: CryptoKey, data: ArrayBuffer):Promise<ArrayBuffer> {
5757
const signature = await crypto.subtle.sign(
5858
"HMAC",
5959
key, // 私钥
6060
data,
6161
);
6262
return signature;
6363
}
64-
async sign(data: ArrayBuffer) {
64+
public async sign(data: ArrayBuffer):Promise<ArrayBuffer> {
6565
await this.initCryptoKey();
6666

6767
return HMAC.sign(this.#cryptoKey!, data);
@@ -70,7 +70,7 @@ export class HMAC {
7070
publicKey: CryptoKey | ArrayBuffer | Uint8Array,
7171
signature: ArrayBuffer,
7272
data: ArrayBuffer,
73-
) {
73+
):Promise<boolean> {
7474
if (publicKey instanceof ArrayBuffer) {
7575
publicKey = await HMAC.parseKey(publicKey);
7676
}
@@ -82,7 +82,7 @@ export class HMAC {
8282
);
8383
return isValid;
8484
}
85-
async verify(signature: ArrayBuffer, data: ArrayBuffer) {
85+
public async verify(signature: ArrayBuffer, data: ArrayBuffer):Promise<boolean> {
8686
await this.initCryptoKey();
8787
return HMAC.verify(this.#cryptoKey!, signature, data);
8888
}

0 commit comments

Comments
 (0)