AUTHOR: xebps
TITLE: Variable Length Hash Function (可変長ハッシュ関数)
STATUS: Publish
ALLOW COMMENTS: 1
CONVERT BREAKS: default
ALLOW PINGS: 1
PRIMARY CATEGORY: 独自技術
CATEGORY: 独自技術
DATE: 10/01/2011 02:41:38
private byte[] vlhf(int hashlen, byte[] srcdata) { //フェーズ1 byte[] rtnhash = new byte[hashlen]; byte num = (byte)(hashlen & 0xFF); int x, y, s, t; byte[,] tbl = new byte[16, 16]; s = srcdata.Length & 0xFF; x = (srcdata.Length & 0xF0) >> 4; y = srcdata.Length & 0x0F; tbl[x, y] = num; t = x; x = y; y = t; for (int i = 1; i < 256; i ++){ for (; (x * 16 + y) == s || tbl[x, y] != 0; ){ if (++y == 16){ y = 0; if (++x == 16) x = 0; } } if (num < 0xFF) num++; else num = 0; tbl[x, y] = num; t = x; x = y; y = t; } //フェーズ2 int cnt,subcnt,len; for (cnt = 0; cnt < srcdata.Length; cnt += Math.Min(1048576, srcdata.Length - cnt)){ len = Math.Min(1048576, srcdata.Length - cnt); for (subcnt = 0; subcnt < len; subcnt ++){ rtnhash[(cnt + subcnt) % hashlen] ^= srcdata[cnt + subcnt]; rtnhash[(cnt + subcnt + 1) % hashlen] ^= srcdata[cnt + len - 1 - subcnt]; rtnhash[(cnt + subcnt + 2) % hashlen] ^= (byte)(rtnhash[(cnt + subcnt) % hashlen] ^ rtnhash[(cnt + subcnt + 1) % hashlen]); rtnhash[(cnt + subcnt) % hashlen] ^= tbl[(rtnhash[(cnt + subcnt + 2) % hashlen] + subcnt) % 16, ((rtnhash[(cnt + subcnt + 2) % hashlen] + subcnt) >> 4) % 16]; rtnhash[(cnt + subcnt + 3) % hashlen] ^= tbl[((rtnhash[(cnt + subcnt + 2) % hashlen] + subcnt) >> 4) % 16, (rtnhash[(cnt + subcnt + 2) % hashlen] + subcnt) % 16]; } } if (cnt < hashlen){ for (; cnt < hashlen; cnt ++){ rtnhash[cnt] ^= tbl[((rtnhash[cnt - 1] / 16) + cnt) % 16, ((rtnhash[cnt - 1] % 16) + cnt) % 16]; }  } for (cnt = 0; cnt < hashlen; cnt++){   rtnhash[(cnt + 1) % hashlen] ^= tbl[rtnhash[cnt] % 16, rtnhash[cnt] / 16];   rtnhash[(hashlen - 1) - ((cnt + 1) % hashlen)] ^= tbl[rtnhash[hashlen - 1 - cnt] / 16, rtnhash[hashlen - 1 - cnt] % 16]; } return rtnhash; } |
private byte[] vlhf(int hashlen, FileStream srcfs) { //フェーズ1 byte[] rtnhash = new byte[hashlen]; byte num = (byte)(hashlen & 0xFF); int x, y, s, t; byte[,] tbl = new byte[16, 16]; s = (int)srcfs.Length & 0xFF; x = ((int)srcfs.Length & 0xF0) >> 4; y = (int)srcfs.Length & 0x0F; tbl[x, y] = num; t = x; x = y; y = t; for (int i = 1; i < 256; i ++){ for (; (x * 16 + y) == s || tbl[x, y] != 0; ){ if (++y == 16){ y = 0; if (++x == 16) x = 0; } } if (num < 0xFF) num++; else num = 0; tbl[x, y] = num; t = x; x = y; y = t; } //フェーズ2 int cnt,subcnt,len; byte[] srctmp = new byte[1048576]; for (cnt = 0; cnt < (int)srcfs.Length; cnt += Math.Min(1048576, (int)srcfs.Length - cnt)){ srcfs.Read(srctmp, 0, Math.Min(1048576, (int)srcfs.Length - cnt)); len = Math.Min(1048576, (int)srcfs.Length - cnt); for (subcnt = 0; subcnt < len; subcnt ++){ rtnhash[(cnt + subcnt) % hashlen] ^= srctmp[subcnt]; rtnhash[(cnt + subcnt + 1) % hashlen] ^= srctmp[len - 1 - subcnt]; rtnhash[(cnt + subcnt + 2) % hashlen] ^= (byte)(rtnhash[(cnt + subcnt) % hashlen] ^ rtnhash[(cnt + subcnt + 1) % hashlen]); rtnhash[(cnt + subcnt) % hashlen] ^= tbl[(rtnhash[(cnt + subcnt + 2) % hashlen] + subcnt) % 16, ((rtnhash[(cnt + subcnt + 2) % hashlen] + subcnt) >> 4) % 16]; rtnhash[(cnt + subcnt + 3) % hashlen] ^= tbl[((rtnhash[(cnt + subcnt + 2) % hashlen] + subcnt) >> 4) % 16, (rtnhash[(cnt + subcnt + 2) % hashlen] + subcnt) % 16]; } } if (cnt < hashlen){ for (; cnt < hashlen; cnt ++){ rtnhash[cnt] ^= tbl[((rtnhash[cnt - 1] / 16) + cnt) % 16, ((rtnhash[cnt - 1] % 16) + cnt) % 16]; }  } for (cnt = 0; cnt < hashlen; cnt++){   rtnhash[(cnt + 1) % hashlen] ^= tbl[rtnhash[cnt] % 16, rtnhash[cnt] / 16];   rtnhash[(hashlen - 1) - ((cnt + 1) % hashlen)] ^= tbl[rtnhash[hashlen - 1 - cnt] / 16, rtnhash[hashlen - 1 - cnt] % 16]; } srcfs.Close(); return rtnhash; } |