google의 guava의 hash 함수가 존재한다.
패키지명은 com.google.common.hash.Hashing이다.
아주 친절하게도 http://goo.gl/jS7HH에 잘 저장되어 있어서 어느 것을 선택할 지 알려주고 있다!!
adler32, crc32,sha256 등등의 경우는 추천하지 않고 있다. Not stable것도 있으니 노트를 잘 보고 용도에 맞게 써야 할 것 같다.
예를 들어, Hashing.murmur3_32는 다음 메소드를 가지고 있다.
/**
* Returns a hash function implementing the
* <a href="http://smhasher.googlecode.com/svn/trunk/MurmurHash3.cpp">
* 32-bit murmur3 algorithm, x86 variant</a> (little-endian variant),
* using the given seed value.
*
* <p>The exact C++ equivalent is the MurmurHash3_x86_32 function (Murmur3A).
*/
public static HashFunction murmur3_32(int seed) {
return new Murmur3_32HashFunction(seed);
}
/**
* Returns a hash function implementing the
* <a href="http://smhasher.googlecode.com/svn/trunk/MurmurHash3.cpp">
* 32-bit murmur3 algorithm, x86 variant</a> (little-endian variant),
* using a seed value of zero.
*
* <p>The exact C++ equivalent is the MurmurHash3_x86_32 function (Murmur3A).
*/
public static HashFunction murmur3_32() {
return Murmur3_32Holder.MURMUR3_32;
}
다음과 같이 사용할 수 있다.
com.google.common.hash.HashFunction algorithm = com.google.common.hash.Hashing.murmur3_32(0);
long b = algorithm.hashBytes("google:plus:201612123:na".getBytes()).padToLong()
참고로 jedis에도 hashing 코드가 존재한다.
https://github.com/xetorthio/jedis/blob/master/src/main/java/redis/clients/util/Hashing.java
다음과 같이 사용할 수 있다.
redis.clients.util.Hashing algorithm = redis.clients.util.Hashing.MURMUR_HASH;
long b = algorithm.hash(redis.clients.util.SafeEncoder.encode("google:plus:20161223:na"));
'general java' 카테고리의 다른 글
nitialize Unable to obtain CGLib fast class and/or method implementation 해결하기 (0) | 2017.01.14 |
---|---|
ObjectMapper, UnrecognizedPropertyException, JsonInclude 예시 (0) | 2016.12.17 |
[jenkins] 사용할 수 있는 jenkins 환경 변수 (0) | 2016.08.20 |
[jenkins] Waiting for next available executor 해결하기 (0) | 2016.08.11 |
[jenkins] Aborted by anonymous, jenkins Finished: ABORTED 해결하기 (0) | 2016.08.10 |