All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.penglecode.common.support.HashAlgorithm Maven / Gradle / Ivy

Go to download

commons is a little java tool to make your development easier in your work.

The newest version!
package com.penglecode.common.support;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 哈希算法
 * 
 * @author	  	pengpeng
 * @date	  	2015年1月14日 下午1:19:59
 * @version  	1.0
 */
public enum HashAlgorithm {

	KETAMA_HASH;
	
	public long hash(final String k) {
		long rv = 0;
		switch (this) {
		case KETAMA_HASH:
			byte[] bKey = computeMd5(k);
			rv = (long) (bKey[3] & 0xFF) << 24 | (long) (bKey[2] & 0xFF) << 16
					| (long) (bKey[1] & 0xFF) << 8 | bKey[0] & 0xFF;
			break;
		default:
			assert false;
		}

		return rv & 0xffffffffL; /* Truncate to 32-bits */
	}

	public static byte[] computeMd5(String key) {
		MessageDigest md5;
		try {
			md5 = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			throw new RuntimeException("MD5 not supported", e);
		}
		md5.reset();
		byte[] keyBytes = null;
		try {
			keyBytes = key.getBytes("UTF-8");
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException("Unsupported encoding!", e);
		}
		
		md5.update(keyBytes);
		return md5.digest();
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy