/* c-basic-offset: 2; tab-width: 2; indent-tabs-mode: nil
 * vi: set shiftwidth=2 tabstop=2 expandtab:
 * :indentSize=2:tabSize=2:noTabs=true:
 */

package common;

import org.bouncycastle.jcajce.provider.digest.*;
import org.bouncycastle.util.encoders.*;
import java.security.MessageDigestSpi;


public class
Utils {
  private Utils () {};

  /**
   * A hash function that salts the password
   * in a way that is pseudo dynamic.
   * 
   * @param t the text to salt
   * @param s the salt
   * @return
   */
  static public String
  real_hash_func (String t, String s) {
    int a = t.getBytes()[0];
    int b = s.getBytes()[a % s.length()] % s.length ();

    StringBuilder sb = new StringBuilder ();
    sb.append (t).insert (b, s);

    SHA3.DigestSHA3 md = new SHA3.DigestSHA3 (512);
    try {
      md.update (sb.toString().getBytes("UTF-8"));
    } catch (Exception e) {
     md.update (sb.toString().getBytes());
    }
    return Hex.toHexString (md.digest ());
  }

  /**
   * Hash a text with a salt.
   * 
   * @param t
   * @return
   */
  static public String
  hash_func (String t) {
    return real_hash_func(t, common.Config.PW_SALT);
  }


}
