Sharkey/src/misc/id/aid.ts

24 lines
573 B
TypeScript
Raw Normal View History

2019-04-13 16:08:26 +00:00
// AID
// 長さ8の[2000年1月1日からの経過ミリ秒をbase36でエンコードしたもの] + 長さ2の[ノイズ文字列]
2020-01-26 20:37:53 +00:00
import * as crypto from 'crypto';
2019-04-13 16:08:26 +00:00
const TIME2000 = 946684800000;
2020-01-26 20:37:53 +00:00
let counter = crypto.randomBytes(2).readUInt16LE(0);
2019-04-13 16:08:26 +00:00
function getTime(time: number) {
time = time - TIME2000;
if (time < 0) time = 0;
return time.toString(36).padStart(8, '0');
}
2019-04-13 17:36:00 +00:00
function getNoise() {
2019-04-13 17:33:50 +00:00
return counter.toString(36).padStart(2, '0').slice(-2);
2019-04-13 16:08:26 +00:00
}
export function genAid(date: Date): string {
counter++;
2019-04-13 17:36:00 +00:00
return getTime(date.getTime()) + getNoise();
2019-04-13 16:08:26 +00:00
}