make v1 be more random

This commit is contained in:
blek 2023-09-11 23:26:01 +10:00
parent d0c95f579e
commit 7ae198bc9c
Signed by: blek
GPG Key ID: 14546221E3595D0C
1 changed files with 10 additions and 6 deletions

View File

@ -1,5 +1,9 @@
import { random } from "./random";
export async function sha256(data: string) {
return await bufferToBase64(await crypto.subtle.digest('SHA-256', new TextEncoder().encode(data)));
}
export async function bufferToBase64(buffer: BufferSource) {
const base64url: string = await new Promise(r => {
const reader = new FileReader()
@ -64,14 +68,14 @@ export class V1 {
scrambled += String.fromCharCode(random(32, 128)) + letter;
}
return {
data: await bufferToBase64(await AES256.encodeStr(scrambled, this.aes256secret)),
made_at: Date.now()
}
return [
(await sha256(Math.random().toString())).substring(0, 6),
await bufferToBase64(await AES256.encodeStr(scrambled, this.aes256secret))
]
}
static async decode(data: { data: string }): Promise<string> {
const encrypted = base64ToArrayBuffer(data.data);
static async decode(data: [string, string]): Promise<string> {
const encrypted = base64ToArrayBuffer(data[1]);
const scrambled = await AES256.decodeToStr(encrypted, this.aes256secret);
let unscrambled = '';
for (let i = 0; i != scrambled.length; i++) {