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