MurmurHash 是一種非加密型哈希函數,適用於通常的哈希檢索操做。與其它流行的哈希函數相比,對於規律性較強的 key,MurmurHash 的隨機分佈特徵表現更良好。java
MurmurHash1, MurmurHash2, MurmurHash3
>>> import binascii >>> import mmh3 >>> binascii.b2a_hex(mmh3.hash_bytes('CN305183362S')).decode('utf8') 'a4fb17cba6d455e4812ad28989780cbc' # 32個字符,128 bit >>> hex(mmh3.hash128('CN305183362S')) '0xbc0c788989d22a81e455d4a6cb17fba4'
import java.math.BigInteger; import org.apache.commons.codec.digest.MurmurHash3; import org.apache.commons.lang.ArrayUtils; public class AppTester { public static void main(String[] args) { final byte[] origin = "CN305183362S".getBytes(); long[] vec = MurmurHash3.hash128(origin, 0, origin.length, 0); // 將 long 轉換爲 BigInteger BigInteger bigInteger0 = BigInteger.valueOf(vec[0]); BigInteger bigInteger1 = BigInteger.valueOf(vec[1]); // 將 BigInteger 轉換爲 byte[] byte[] array0 = bigInteger0.toByteArray(); byte[] array1 = bigInteger1.toByteArray(); // 反轉 byte[](大小端轉換) ArrayUtils.reverse(array0); ArrayUtils.reverse(array1); // 將 byte[] 轉換爲無符號整數,並轉爲十六進制字符串 String part0 = (new BigInteger(1, array0)).toString(16); String part1 = (new BigInteger(1, array1)).toString(16); System.out.println(part0 + part1); // a4fb17cba6d455e4812ad28989780cbc } }
import java.nio.ByteBuffer; import org.apache.commons.codec.digest.MurmurHash3; import org.apache.commons.lang.ArrayUtils; public class AppTester { public static void main(String[] args) { final byte[] origin = "CN305183362S".getBytes(); long[] vec = MurmurHash3.hash128(origin, 0, origin.length, 0); ByteBuffer buf0 = ByteBuffer.allocate(8); ByteBuffer buf1 = ByteBuffer.allocate(8); buf0.putLong(0, vec[0]); buf1.putLong(0, vec[1]); byte[] array0 = buf0.putLong(0, vec[0]).array(); byte[] array1 = buf1.putLong(0, vec[1]).array(); ArrayUtils.reverse(array0); // 反轉 byte[](大小端轉換) ArrayUtils.reverse(array1); // 反轉 byte[](大小端轉換) buf0.put(array0, 0, array0.length).flip(); buf1.put(array1, 0, array1.length).flip(); String part0 = String.format("%x", buf0.getLong()); String part1 = String.format("%x", buf1.getLong()); System.out.println(part0 + part1); // a4fb17cba6d455e4812ad28989780cbc } }
import org.apache.commons.codec.digest.MurmurHash3; public class AppTester { public static void main(String[] args) { final byte[] origin = "CN305183362S".getBytes(); long[] vec = MurmurHash3.hash128(origin, 0, origin.length, 0); String part0 = String.format("%x", vec[0]); String part1 = String.format("%x", vec[1]); String line = ""; // 反轉前半段(大小端轉換) for (int i = 0; i < 8; ++i) { line += part0.substring(14 - 2 * i, 16 - 2 * i); } // 反轉後半段(大小端轉換) for (int i = 0; i < 8; ++i) { line += part1.substring(14 - 2 * i, 16 - 2 * i); } System.out.println(line); // a4fb17cba6d455e4812ad28989780cbc } }
var murmurHash3 = require("murmurhash3js"); let line = murmurHash3.x64.hash128("CN305183362S"); console.log(line); // e455d4a6cb17fba4bc0c788989d22a81 let newLine = ""; // 反轉前半段(大小端轉換) for (let i = 0; i < 8; ++i) { newLine += line.slice(14-2*i, 16-2*i); } // 反轉後半段(大小端轉換) for (let i = 0; i < 8; ++i) { newLine += line.slice(30-2*i, 32-2*i); } console.log(newLine); // a4fb17cba6d455e4812ad28989780cbc
本文出處 walker snapshot