From c25aa1b374525d38d5adb19d12eb870a13e62fae Mon Sep 17 00:00:00 2001 From: kakkokari-gtyih Date: Sun, 21 Jan 2024 15:44:03 +0900 Subject: [PATCH] Refactor AID generation logic --- assets/js/mi/aid.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/assets/js/mi/aid.ts b/assets/js/mi/aid.ts index d29d52f2..f1ddc74a 100644 --- a/assets/js/mi/aid.ts +++ b/assets/js/mi/aid.ts @@ -5,18 +5,16 @@ // AID // 長さ8の[2000年1月1日からの経過ミリ秒をbase36でエンコードしたもの] + 長さ2の[ノイズ文字列] +import { customAlphabet } from 'nanoid'; + export const aidRegExp = /^[0-9a-z]{10}$/; +const rand = customAlphabet('0123456789', 5); + const TIME2000 = 946684800000; let counter: number; -if (process.client) { - const arr = window.crypto.getRandomValues(new Uint16Array(2)); - counter = parseInt(arr[0].toString()); -} else { - const crypto = await import('crypto'); - counter = crypto.randomBytes(2).readUInt16LE(0); -} +counter = parseInt(rand(), 10); function getTime(time: number): string { time = time - TIME2000;