/*
 * Created on 2003/07/02
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package org.j69.eewiki.util;

import java.security.MessageDigest;

/*
 * Copyright 1997-2001 by Sun Microsystems, Inc.,
 * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of Sun Microsystems, Inc. ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with Sun.
 */
/**
 * This program demonstrates how to generate a secret-key object for
 * HMAC-MD5, and initialize an HMAC-MD5 object with it.
 */
public class SimpleCrypt {

	public static String genMD5String(String passwd) throws Exception {

		// Generate secret key for HMAC-MD5
//		KeyGenerator kg = KeyGenerator.getInstance("HmacMD5");
//		SecretKey sk = kg.generateKey();
//
//		// Get instance of Mac object implementing HMAC-MD5, and 
//		// initialize it with the above secret key
//		Mac mac = Mac.getInstance("HmacMD5");
//		mac.init(sk);
//		byte[] result = mac.doFinal(passwd.getBytes());

		//SHAのMessageDigestを作成
		MessageDigest md = MessageDigest.getInstance("SHA");
		//与えられたデータでMessageDigestを更新
		md.update(passwd.getBytes());
		//MessageDigestからダイジェスト値を獲得
		byte[] digest = md.digest();
				
		//暗号はStringオブジェクトに格納してはいけないと言うけれど...
		StringBuffer sb = new StringBuffer();
		for (int i = 0 ; i < digest.length - 1 ; i++) {
				sb.append(digest[i]);
		}
		return sb.toString();
	}
}
