Skip to content

Commit 8db84cd

Browse files
committed
first commit add template codes
1 parent 3b01038 commit 8db84cd

21 files changed

+2548
-1
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules/
2+
3+
dist/

.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"editor.codeActionsOnSave": {
3+
"source.organizeImports": "explicit"
4+
}
5+
}

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
same "printed page" as the copyright notice for easier
187187
identification within third-party archives.
188188

189-
Copyright [yyyy] [name of copyright owner]
189+
Copyright (c) 2024-present datadata.com
190190

191191
Licensed under the Apache License, Version 2.0 (the "License");
192192
you may not use this file except in compliance with the License.

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# datadata sdk javascript
2+
3+
javascript sdk for [datadata](https://www.datadata.com) platform.
4+
5+
Support browser, nodejs.
6+
7+
## Installation
8+
9+
```shell
10+
pnpm add @datadata/sdk-javascript
11+
```

index.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Document</title>
7+
<script type="module" src="./src/main.ts"></script>
8+
</head>
9+
<body>
10+
Working...
11+
</body>
12+
</html>

lib/api-key/api-key.service.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// test("abc", () => {
2+
// const payload: APITokenPayload = {
3+
// uid: "abc",
4+
// host: "www.example.com",
5+
// expired: Date.now() + 3600,
6+
// };
7+
// const token = APIKeyService.encrypt("500c33c5485e4d7eb5c89dd8f33084dc", JSON.stringify(payload));
8+
// console.log(token);
9+
// });

lib/api-key/api-key.service.ts

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import AES from "crypto-js/aes";
2+
import Base64 from "crypto-js/enc-base64";
3+
import Hex from "crypto-js/enc-hex";
4+
import Utf8 from "crypto-js/enc-utf8";
5+
import WordArray from "crypto-js/lib-typedarrays";
6+
import MD5 from "crypto-js/md5";
7+
import CFB from "crypto-js/mode-cfb";
8+
import Pkcs7 from "crypto-js/pad-pkcs7";
9+
import { BaseService } from "../common";
10+
11+
export class APIKeyService extends BaseService {
12+
public static async encrypt(secretKey: string, payload: string) {
13+
const iv = WordArray.random(16);
14+
const key = MD5(secretKey);
15+
const value = Utf8.parse(payload).clone().concat(MD5(payload));
16+
// const value = Utf8.parse(payload);
17+
18+
console.log(value.sigBytes);
19+
20+
const encrypted = AES.encrypt(value, key, { iv: iv, mode: CFB, padding: Pkcs7 });
21+
22+
console.log({
23+
a: MD5(payload),
24+
b: iv.clone().concat(encrypted.ciphertext),
25+
});
26+
27+
return iv.clone().concat(encrypted.ciphertext).toString(Hex);
28+
}
29+
30+
public static async decrypt(secretKey: string, ciphertext: string) {
31+
const key = MD5(secretKey);
32+
const cipherData = Hex.parse(ciphertext);
33+
const iv = WordArray.create(cipherData.words.slice(0, 4), 16);
34+
const data = WordArray.create(cipherData.words.slice(4), cipherData.sigBytes - 16);
35+
const decrypted = AES.decrypt(data.toString(Base64), key, { iv: iv, mode: CFB });
36+
37+
console.log({ decrypted });
38+
39+
return decrypted.toString(Utf8);
40+
}
41+
}
42+
43+
function CryptJsWordArrayToUint8Array(wordArray: WordArray) {
44+
const l = wordArray.sigBytes;
45+
const words = wordArray.words;
46+
const result = new Uint8Array(l);
47+
var i = 0 /*dst*/,
48+
j = 0; /*src*/
49+
while (true) {
50+
// here i is a multiple of 4
51+
if (i == l) break;
52+
var w = words[j++];
53+
result[i++] = (w & 0xff000000) >>> 24;
54+
if (i == l) break;
55+
result[i++] = (w & 0x00ff0000) >>> 16;
56+
if (i == l) break;
57+
result[i++] = (w & 0x0000ff00) >>> 8;
58+
if (i == l) break;
59+
result[i++] = w & 0x000000ff;
60+
}
61+
return result;
62+
}
63+
64+
function hexStringToUint8Array(hexString: string) {
65+
if (hexString.length % 2 !== 0) {
66+
throw "Invalid hexString";
67+
} /*from w w w. j av a 2s . c o m*/
68+
var arrayBuffer = new Uint8Array(hexString.length / 2);
69+
70+
for (var i = 0; i < hexString.length; i += 2) {
71+
var byteValue = parseInt(hexString.substr(i, 2), 16);
72+
if (isNaN(byteValue)) {
73+
throw "Invalid hexString";
74+
}
75+
arrayBuffer[i / 2] = byteValue;
76+
}
77+
78+
return arrayBuffer;
79+
}
80+
81+
function convertUint8ArrayToWordArray(u8Array: Uint8Array): WordArray {
82+
var words = [],
83+
i = 0,
84+
len = u8Array.length;
85+
while (i < len) {
86+
words.push((u8Array[i++] << 24) | (u8Array[i++] << 16) | (u8Array[i++] << 8) | u8Array[i++]);
87+
}
88+
return WordArray.create(words, u8Array.length);
89+
}

lib/api-key/api-key.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export interface APIKey {
2+
id: string;
3+
name: string;
4+
userId: string;
5+
secretKey: string;
6+
accessKey: string;
7+
expiration: string;
8+
}

lib/api-key/api-token-payload.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export interface APITokenPayload {
2+
uid?: string;
3+
host?: string;
4+
expired?: number;
5+
}

lib/api-key/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from "./api-key";
2+
export * from "./api-key.service";
3+
export * from "./api-token-payload";

0 commit comments

Comments
 (0)