📄 myrabin.java
字号:
class MyRabin{
private static final int DEFAULT_IRREDUCIBLE_POLY = 0x0000008D;
//public static final temp DEFAULT_HASH_FUNCTION =new temp(DEFAULT_IRREDUCIBLE_POLY);
private static final int P_DEGREE = 32;
private static final int X_P_DEGREE = 1 << (P_DEGREE - 1);
private static final int READ_BUFFER_SIZE = 1024;
private final int P= 0x0000008D;
private int[] table32, table40, table48, table56;
private int hash1;
MyRabin(){
final int[] mods = new int[P_DEGREE];
mods[0] = P;
for (int i = 1; i < P_DEGREE; i++) {
final int lastMod = mods[i - 1];
int thisMod = lastMod << 1;
if ((lastMod & X_P_DEGREE) != 0) {
thisMod ^= P;
}
mods[i] = thisMod;
}
table32 = new int[256];
table40 = new int[256];
table48 = new int[256];
table56 = new int[256];
for (int i = 0; i < 256; i++) {
int c = i;
for (int j = 0; j < 8 && c > 0; j++) {
if ((c & 1) != 0) {
table32[i] ^= mods[j];
table40[i] ^= mods[j + 8];
table48[i] ^= mods[j + 16];
table56[i] ^= mods[j + 24];
}
c >>>= 1;
}
}
}//endtemp2
//private final temp rhf = temp.DEFAULT_HASH_FUNCTION;
protected void engineUpdate(final byte[] input, final int offset, final int len) {
hash1= hash(input, offset, len, hash1);
}
protected void engineUpdatec(final char[] input,final int offset,final int len){
hash1=hash(input);
}
protected byte[] engineDigest() {
final byte[] buf = new byte[4];
hashToBuf(buf, 0);
engineReset();
return buf;
}
protected void engineReset() {
hash1= 0;
}
private void hashToBuf(final byte[] buf, final int offset) {
buf[offset] = (byte) (hash1 >> 24);
buf[offset + 1] = (byte) (hash1 >> 16);
buf[offset + 2] = (byte) (hash1 >> 8);
buf[offset + 3] = (byte) hash1;
}
private int computeWShifted(final int w) {
return table32[w & 0xFF] ^
table40[(w >>> 8) & 0xFF] ^
table48[(w >>> 16) & 0xFF] ^
table56[(w >>> 24) & 0xFF];
}
int hash(final byte[] A, final int offset, final int length, int w) {
int s = offset;
// First, process a few bytes so that the number of bytes remaining is a multiple of 4.
// This makes the later loop easier.
final int starterBytes = length % 4;
if (starterBytes != 0) {
final int max = offset + starterBytes;
while (s < max) {
w = (w << 8) ^ (A[s] & 0xFF);
s++;
}
}
final int max = offset + length;
while (s < max) {
w = computeWShifted(w) ^
(A[s] << 24) ^
((A[s + 1] & 0xFF) << 16) ^
((A[s + 2] & 0xFF) << 8) ^
(A[s + 3] & 0xFF);
s += 4;
}
return w;
}
public int hash(final char[] A) {
int w, s;
// If an odd number of characters, process the first char so that the number remaining
// is a multiple of 2. This makes the later loop easier.
if (A.length % 2 == 1) {
w = A[0] & 0xFFFF;
s = 1;
} else {
w = 0;
s = 0;
}
while (s < A.length) {
w = computeWShifted(w) ^ ((A[s] & 0xFFFF) << 16) ^ (A[s + 1] & 0xFFFF);
s += 2;
}
return w;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -