0.0.3Updated 17 days ago
import { pbkdf2Sync, randomBytes } from 'node:crypto';

console.log(Deno.args);

const Hash = (input: string, salt: string) => {
  try {
    return pbkdf2Sync(input, salt, 10000, 64, 'sha512').toString('hex');
  // deno-lint-ignore no-explicit-any
  } catch(e: any) {
    console.error(`%cInvalid inputs provided.%c [${e.message}]`, 'color: orange', '');
    Deno.exit(1);
  }
}

const hashPassword = (password: string) => {
  if(!password) return;

  const salt = randomBytes(32).toString('hex');
  const hash = Hash(password, salt);

  return `${hash}:${salt}`;
}

const checkPassword = (password: string, password_hash: string) => {
  if(!password_hash) return false;

  const [hash, salt] = password_hash.split(':');
  const check_hash = Hash(password, salt);

  return hash == check_hash;
}

const check = Deno.args.includes('--check') || Deno.args.includes('-C');
const [password, password_hash] = Deno.args.filter(arg => !['--check', '-C'].includes(arg));

console.log(check, password, password_hash);

if(check) {
  const pass = checkPassword(password, password_hash);
  if(pass) {
    console.log('%cPASS', 'color: green');
  } else {
    console.log('%cFAIL', 'color: red');
  }
  Deno.exit(0);
}

hashPassword(password);