guaranteed_security
Version:
Sebuah package NPM untuk mendeteksi dan memfilter pesan bug/crash/spam pada bot WhatsApp (Baileys) dan menjaga bot mu dalam kondisi terkunci| Telah Dimodifikasi
67 lines (52 loc) • 1.86 kB
text/typescript
import fs from 'fs'
import path from 'path'
import crypto from 'crypto'
const digits = '0123456789'
const lowerCase = 'abcdefghijklmnopqrstuvwxyz'
const upperCase = lowerCase.toUpperCase()
const special = `@#$_&-+()/*"':;!?<>[{}=~\\\`|•√π÷×§∆\\✓™®©%£¢€¥^°`
const allChars: string = digits + lowerCase + upperCase + special
const tokenDir = path.join(process.cwd(), 'Rexxzy-security-code')
if (!fs.existsSync(tokenDir)) fs.mkdirSync(tokenDir, { recursive: true })
export interface TokenMeta {
hash: string
createdAt: number
expiresAt: number
used: boolean
}
/**
* Hanya menghasilkan dan menyimpan hash token (raw tidak disimpan)
* Raw token dikembalikan agar bisa diberikan secara manual
*/
export function generateOneTimeToken(length = 32, ttlMs = 3600000): { raw: string, meta: TokenMeta } {
let raw = ''
while (raw.length < length) {
const rand = crypto.randomInt(0, allChars.length)
raw += allChars[rand]
}
const hash = crypto.createHash('sha256').update(raw).digest('hex')
const now = Date.now()
const meta: TokenMeta = {
hash,
createdAt: now,
expiresAt: now + ttlMs,
used: false,
}
fs.writeFileSync(path.join(tokenDir, `${hash}.json`), JSON.stringify(meta, null, 2))
return { raw, meta }
}
/**
* Validasi token berdasarkan input RAW user
* True jika valid, aktif, dan belum dipakai
*/
export function verifyOneTimeToken(inputRaw: string): boolean {
const hash = crypto.createHash('sha256').update(inputRaw).digest('hex')
const file = path.join(tokenDir, `${hash}.json`)
if (!fs.existsSync(file)) return false
const meta: TokenMeta = JSON.parse(fs.readFileSync(file, 'utf-8'))
if (meta.used || Date.now() > meta.expiresAt) return false
// Tandai sudah dipakai
meta.used = true
fs.writeFileSync(file, JSON.stringify(meta, null, 2))
return true
}