complete V1

This commit is contained in:
blek 2023-09-11 22:51:28 +10:00
parent da5b74b5f1
commit d0c95f579e
Signed by: blek
GPG Key ID: 14546221E3595D0C
1 changed files with 24 additions and 2 deletions

View File

@ -11,6 +11,15 @@ export async function bufferToBase64(buffer: BufferSource) {
return base64url.slice(base64url.indexOf(',') + 1); return base64url.slice(base64url.indexOf(',') + 1);
} }
export function base64ToArrayBuffer(base64: string) {
var binaryString = atob(base64);
var bytes = new Uint8Array(binaryString.length);
for (var i = 0; i < binaryString.length; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
return bytes.buffer;
}
export class AES256 { export class AES256 {
static async str2key(key: string): Promise<CryptoKey> { static async str2key(key: string): Promise<CryptoKey> {
if (key.length > 32) if (key.length > 32)
@ -56,9 +65,22 @@ export class V1 {
} }
return { return {
data: await bufferToBase64(await AES256.encodeStr(scrambled, this.aes256secret)) data: await bufferToBase64(await AES256.encodeStr(scrambled, this.aes256secret)),
made_at: Date.now()
} }
} }
static async decode(data: { data: string }): Promise<string> {
const encrypted = base64ToArrayBuffer(data.data);
const scrambled = await AES256.decodeToStr(encrypted, this.aes256secret);
let unscrambled = '';
for (let i = 0; i != scrambled.length; i++) {
if (i % 2 == 1) {
unscrambled += scrambled[i];
}
}
return unscrambled;
}
} }
export function encode(word: string) { export function encode(word: string) {