import * as bcrypt from 'bcrypt-nodejs' ; import { base64encode , base64decode } from '../helpers/helpers' ; /**
 *
 */ export class UserTimedId { param ns param ttlInSec constructor ( private ns , private ttlInSec = 3600 ) { } /**
     *
     */ private getCurrentTime ( ) { return ~ ~ ( + new Date ( ) / 1000 ) ; } param user param timer private getCurrentRestIdForUser ( nonce , timer ) { return this . ns + nonce + timer ; } param user public generateId ( nonce ) { return base64encode ( bcrypt . hashSync ( this . getCurrentRestIdForUser ( nonce , ~ ~ ( this . getCurrentTime ( ) / this . ttlInSec ) ) ) ) ; } param id param user param startTime public checkId ( id , nonce ) { try { const decodedHash = base64decode ( id ) ; return bcrypt . compareSync ( this . getCurrentRestIdForUser ( nonce , ~ ~ ( ( this . getCurrentTime ( ) - 0 ) / this . ttlInSec ) ) , decodedHash ) || bcrypt . compareSync ( this . getCurrentRestIdForUser ( nonce , ~ ~ ( ( this . getCurrentTime ( ) - this . ttlInSec + 1 ) / this . ttlInSec ) ) , decodedHash ) ; } catch ( err ) { return false ; } } }