package jp.co.y_net.amm.common;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class CalcHash {
//    private static int STRETCH_COUNT = 500;
    public static String passowrdHash(String password, String account) {
//        String salt = getSha256(loginId);
//        String hash = "";
//
//        for (int i = 0; i < STRETCH_COUNT; i++) {
//            hash = getSha256(hash + salt + password);
//        }
//        return hash;
        
        return getSha256(account+":"+password);
    }
    public static String challengeResponse(String phash, String chvalue) {
        //return getSha256(passwordHash + challenge);
        return getSha256(phash+":"+chvalue);
    }
    /*
     * 文字列から SHA256 のハッシュ値を取得
     */
    private static String getSha256(String target) {
        MessageDigest md = null;
        StringBuilder buf = new StringBuilder();
        try {
            md = MessageDigest.getInstance("SHA-256");
            md.update(target.getBytes());
            byte[] digest = md.digest();

            for (int i = 0; i < digest.length; i++) {
                buf.append(String.format("%02x", digest[i]));
            }

        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }

        return buf.toString();
    }
    
    /*---------------------------------------------------------------------------------------------------------------*/
    /* 単体テスト*/
    
    public static void main(String[] args) {
        System.out.println(passowrdHash("p@ssw0rd", "ryoma.sakamoto@example.com"));
        System.out.println(passowrdHash("test", "kondou@example.com"));
        System.out.println(passowrdHash("testtest", "okita@example.com"));
        System.out.println(passowrdHash("testtest", "admin"));
    }

}
