0000: 2f 2a 0a 20 2a 20 54 68 69 73 20 66 69 6c 65 20 /*. * This file
0010: 69 73 20 61 20 70 61 72 74 20 6f 66 20 4a 61 76 is a part of Jav
0020: 61 20 42 69 6e 64 69 6e 67 73 20 66 6f 72 20 4c a Bindings for L
0030: 69 62 72 68 61 73 68 0a 20 2a 20 43 6f 70 79 72 ibrhash. * Copyr
0040: 69 67 68 74 20 28 63 29 20 32 30 31 31 2d 32 30 ight (c) 2011-20
0050: 31 32 2c 20 32 30 31 34 2c 20 53 65 72 67 65 79 12, 2014, Sergey
0060: 20 42 61 73 61 6c 61 65 76 20 3c 73 62 61 73 61 Basalaev <sbasa
0070: 6c 61 65 76 40 67 6d 61 69 6c 2e 63 6f 6d 3e 0a laev@gmail.com>.
0080: 20 2a 20 4c 69 62 72 68 61 73 68 20 69 73 20 28 * Librhash is (
0090: 63 29 20 32 30 31 31 2d 32 30 31 32 2c 20 41 6c c) 2011-2012, Al
00a0: 65 6b 73 65 79 20 4b 72 61 76 63 68 65 6e 6b 6f eksey Kravchenko
00b0: 20 3c 72 68 61 73 68 2e 61 64 6d 69 6e 40 67 6d <rhash.admin@gm
00c0: 61 69 6c 2e 63 6f 6d 3e 0a 20 2a 20 0a 20 2a 20 ail.com>. * . *
00d0: 50 65 72 6d 69 73 73 69 6f 6e 20 69 73 20 68 65 Permission is he
00e0: 72 65 62 79 20 67 72 61 6e 74 65 64 2c 20 66 72 reby granted, fr
00f0: 65 65 20 6f 66 20 63 68 61 72 67 65 2c 20 20 74 ee of charge, t
0100: 6f 20 61 6e 79 20 70 65 72 73 6f 6e 20 6f 62 74 o any person obt
0110: 61 69 6e 69 6e 67 20 61 20 63 6f 70 79 0a 20 2a aining a copy. *
0120: 20 6f 66 20 74 68 69 73 20 73 6f 66 74 77 61 72 of this softwar
0130: 65 20 61 6e 64 20 61 73 73 6f 63 69 61 74 65 64 e and associated
0140: 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66 documentation f
0150: 69 6c 65 73 20 28 74 68 65 20 22 53 6f 66 74 77 iles (the "Softw
0160: 61 72 65 22 29 2c 20 74 6f 20 64 65 61 6c 0a 20 are"), to deal.
0170: 2a 20 69 6e 20 74 68 65 20 53 6f 66 74 77 61 72 * in the Softwar
0180: 65 20 77 69 74 68 6f 75 74 20 72 65 73 74 72 69 e without restri
0190: 63 74 69 6f 6e 2c 20 20 69 6e 63 6c 75 64 69 6e ction, includin
01a0: 67 20 77 69 74 68 6f 75 74 20 6c 69 6d 69 74 61 g without limita
01b0: 74 69 6f 6e 20 74 68 65 20 72 69 67 68 74 73 0a tion the rights.
01c0: 20 2a 20 74 6f 20 20 75 73 65 2c 20 20 63 6f 70 * to use, cop
01d0: 79 2c 20 20 6d 6f 64 69 66 79 2c 20 20 6d 65 72 y, modify, mer
01e0: 67 65 2c 20 70 75 62 6c 69 73 68 2c 20 64 69 73 ge, publish, dis
01f0: 74 72 69 62 75 74 65 2c 20 73 75 62 6c 69 63 65 tribute, sublice
0200: 6e 73 65 2c 20 61 6e 64 2f 6f 72 20 73 65 6c 6c nse, and/or sell
0210: 0a 20 2a 20 63 6f 70 69 65 73 20 20 6f 66 20 20 . * copies of
0220: 74 68 65 20 53 6f 66 74 77 61 72 65 2c 20 20 61 the Software, a
0230: 6e 64 20 20 74 6f 20 70 65 72 6d 69 74 20 20 70 nd to permit p
0240: 65 72 73 6f 6e 73 20 20 74 6f 20 77 68 6f 6d 20 ersons to whom
0250: 20 74 68 65 20 53 6f 66 74 77 61 72 65 20 20 69 the Software i
0260: 73 0a 20 2a 20 66 75 72 6e 69 73 68 65 64 20 74 s. * furnished t
0270: 6f 20 64 6f 20 73 6f 2e 0a 20 2a 20 0a 20 2a 20 o do so.. * . *
0280: 54 68 69 73 20 6c 69 62 72 61 72 79 20 20 69 73 This library is
0290: 20 64 69 73 74 72 69 62 75 74 65 64 20 20 69 6e distributed in
02a0: 20 74 68 65 20 68 6f 70 65 20 74 68 61 74 20 69 the hope that i
02b0: 74 20 77 69 6c 6c 20 62 65 20 75 73 65 66 75 6c t will be useful
02c0: 2c 20 62 75 74 20 57 49 54 48 4f 55 54 0a 20 2a , but WITHOUT. *
02d0: 20 41 4e 59 20 57 41 52 52 41 4e 54 59 3b 20 77 ANY WARRANTY; w
02e0: 69 74 68 6f 75 74 20 65 76 65 6e 20 74 68 65 20 ithout even the
02f0: 69 6d 70 6c 69 65 64 20 77 61 72 72 61 6e 74 79 implied warranty
0300: 20 6f 66 20 4d 45 52 43 48 41 4e 54 41 42 49 4c of MERCHANTABIL
0310: 49 54 59 20 6f 72 20 46 49 54 4e 45 53 53 0a 20 ITY or FITNESS.
0320: 2a 20 46 4f 52 20 41 20 50 41 52 54 49 43 55 4c * FOR A PARTICUL
0330: 41 52 20 50 55 52 50 4f 53 45 2e 20 55 73 65 20 AR PURPOSE. Use
0340: 69 74 20 61 74 20 79 6f 75 72 20 6f 77 6e 20 72 it at your own r
0350: 69 73 6b 21 0a 20 2a 2f 0a 0a 75 73 69 6e 67 20 isk!. */..using
0360: 53 79 73 74 65 6d 3b 0a 0a 6e 61 6d 65 73 70 61 System;..namespa
0370: 63 65 20 52 48 61 73 68 20 7b 0a 09 0a 09 2f 2a ce RHash {..../*
0380: 0a 09 20 2a 20 54 79 70 65 20 6f 66 20 68 61 73 .. * Type of has
0390: 68 69 6e 67 20 61 6c 67 6f 72 69 74 68 6d 2e 0a hing algorithm..
03a0: 09 20 2a 20 53 75 70 70 6f 72 74 65 64 20 61 6c . * Supported al
03b0: 67 6f 72 69 74 68 6d 73 20 61 72 65 20 4d 44 34 gorithms are MD4
03c0: 2c 20 4d 44 35 2c 20 53 48 41 31 2f 53 48 41 32 , MD5, SHA1/SHA2
03d0: 2c 20 54 69 67 65 72 2c 0a 09 20 2a 20 44 43 2b , Tiger,.. * DC+
03e0: 2b 20 54 54 48 2c 20 42 69 74 54 6f 72 72 65 6e + TTH, BitTorren
03f0: 74 20 42 54 49 48 2c 20 41 49 43 48 2c 20 45 44 t BTIH, AICH, ED
0400: 6f 6e 6b 65 79 20 32 30 30 30 20 68 61 73 68 2c onkey 2000 hash,
0410: 20 47 4f 53 54 20 52 20 33 34 2e 31 31 2d 39 34 GOST R 34.11-94
0420: 2c 0a 09 20 2a 20 52 49 50 45 4d 44 2d 31 36 30 ,.. * RIPEMD-160
0430: 2c 20 48 41 53 2d 31 36 30 2c 20 45 44 4f 4e 2d , HAS-160, EDON-
0440: 52 20 32 35 36 2f 35 31 32 2c 20 57 68 69 72 6c R 256/512, Whirl
0450: 70 6f 6f 6c 20 61 6e 64 20 53 6e 65 66 72 75 2d pool and Snefru-
0460: 31 32 38 2f 32 35 36 2e 0a 09 20 2a 2f 0a 09 70 128/256... */..p
0470: 75 62 6c 69 63 20 65 6e 75 6d 20 48 61 73 68 54 ublic enum HashT
0480: 79 70 65 20 3a 20 75 69 6e 74 20 7b 0a 09 09 2f ype : uint {.../
0490: 2a 20 43 52 43 33 32 20 63 68 65 63 6b 73 75 6d * CRC32 checksum
04a0: 2e 20 2a 2f 0a 09 09 43 52 43 33 32 20 3d 20 31 . */...CRC32 = 1
04b0: 2c 0a 09 09 2f 2a 20 4d 44 34 20 68 61 73 68 2e ,.../* MD4 hash.
04c0: 20 2a 2f 0a 09 09 4d 44 34 20 3d 20 31 20 3c 3c */...MD4 = 1 <<
04d0: 20 31 2c 0a 09 09 2f 2a 20 4d 44 35 20 68 61 73 1,.../* MD5 has
04e0: 68 2e 20 2a 2f 0a 09 09 4d 44 35 20 3d 20 31 20 h. */...MD5 = 1
04f0: 3c 3c 20 32 2c 0a 09 09 2f 2a 20 53 48 41 2d 31 << 2,.../* SHA-1
0500: 20 68 61 73 68 2e 20 2a 2f 0a 09 09 53 48 41 31 hash. */...SHA1
0510: 20 3d 20 31 20 3c 3c 20 33 2c 0a 09 09 2f 2a 20 = 1 << 3,.../*
0520: 54 69 67 65 72 20 68 61 73 68 2e 20 2a 2f 0a 09 Tiger hash. */..
0530: 09 54 49 47 45 52 20 3d 20 31 20 3c 3c 20 34 2c .TIGER = 1 << 4,
0540: 0a 09 09 2f 2a 20 54 69 67 65 72 20 74 72 65 65 .../* Tiger tree
0550: 20 68 61 73 68 20 2a 2f 0a 09 09 54 54 48 20 3d hash */...TTH =
0560: 20 31 20 3c 3c 20 35 2c 0a 09 09 2f 2a 20 42 69 1 << 5,.../* Bi
0570: 74 54 6f 72 72 65 6e 74 20 69 6e 66 6f 20 68 61 tTorrent info ha
0580: 73 68 2e 20 2a 2f 0a 09 09 42 54 49 48 20 3d 20 sh. */...BTIH =
0590: 31 20 3c 3c 20 36 2c 0a 09 09 2f 2a 20 45 44 6f 1 << 6,.../* EDo
05a0: 6e 6b 65 79 20 32 30 30 30 20 68 61 73 68 2e 20 nkey 2000 hash.
05b0: 2a 2f 0a 09 09 45 44 32 4b 20 3d 20 31 20 3c 3c */...ED2K = 1 <<
05c0: 20 37 2c 0a 09 09 2f 2a 20 65 4d 75 6c 65 20 41 7,.../* eMule A
05d0: 49 43 48 2e 20 2a 2f 0a 09 09 41 49 43 48 20 3d ICH. */...AICH =
05e0: 20 31 20 3c 3c 20 38 2c 0a 09 09 2f 2a 20 57 68 1 << 8,.../* Wh
05f0: 69 72 6c 70 6f 6f 6c 20 68 61 73 68 2e 20 2a 2f irlpool hash. */
0600: 0a 09 09 57 48 49 52 4c 50 4f 4f 4c 20 3d 20 31 ...WHIRLPOOL = 1
0610: 20 3c 3c 20 39 2c 0a 09 09 2f 2a 20 52 49 50 45 << 9,.../* RIPE
0620: 4d 44 2d 31 36 30 20 68 61 73 68 2e 20 2a 2f 0a MD-160 hash. */.
0630: 09 09 52 49 50 45 4d 44 31 36 30 20 3d 20 31 20 ..RIPEMD160 = 1
0640: 3c 3c 20 31 30 2c 0a 09 09 2f 2a 20 47 4f 53 54 << 10,.../* GOST
0650: 20 52 20 33 34 2e 31 31 2d 39 34 2e 20 2a 2f 0a R 34.11-94. */.
0660: 09 09 47 4f 53 54 20 3d 20 31 20 3c 3c 20 31 31 ..GOST = 1 << 11
0670: 2c 0a 09 09 47 4f 53 54 5f 43 52 59 50 54 4f 50 ,...GOST_CRYPTOP
0680: 52 4f 20 3d 20 31 20 3c 3c 20 31 32 2c 0a 09 09 RO = 1 << 12,...
0690: 2f 2a 20 48 41 53 2d 31 36 30 20 68 61 73 68 2e /* HAS-160 hash.
06a0: 20 2a 2f 0a 09 09 48 41 53 31 36 30 20 3d 20 31 */...HAS160 = 1
06b0: 20 3c 3c 20 31 33 2c 0a 09 09 2f 2a 20 53 6e 65 << 13,.../* Sne
06c0: 66 72 75 2d 31 32 38 20 68 61 73 68 2e 20 2a 2f fru-128 hash. */
06d0: 0a 09 09 53 4e 45 46 52 55 31 32 38 20 3d 20 31 ...SNEFRU128 = 1
06e0: 20 3c 3c 20 31 34 2c 0a 09 09 2f 2a 20 53 6e 65 << 14,.../* Sne
06f0: 66 72 75 2d 32 35 36 20 68 61 73 68 2e 20 2a 2f fru-256 hash. */
0700: 0a 09 09 53 4e 45 46 52 55 32 35 36 20 3d 20 31 ...SNEFRU256 = 1
0710: 20 3c 3c 20 31 35 2c 0a 09 09 2f 2a 20 53 48 41 << 15,.../* SHA
0720: 2d 32 32 34 20 68 61 73 68 2e 20 2a 2f 0a 09 09 -224 hash. */...
0730: 53 48 41 32 32 34 20 3d 20 31 20 3c 3c 20 31 36 SHA224 = 1 << 16
0740: 2c 0a 09 09 2f 2a 20 53 48 41 2d 32 35 36 20 68 ,.../* SHA-256 h
0750: 61 73 68 2e 20 2a 2f 0a 09 09 53 48 41 32 35 36 ash. */...SHA256
0760: 20 3d 20 31 20 3c 3c 20 31 37 2c 0a 09 09 2f 2a = 1 << 17,.../*
0770: 20 53 48 41 2d 33 38 34 20 68 61 73 68 2e 20 2a SHA-384 hash. *
0780: 2f 0a 09 09 53 48 41 33 38 34 20 3d 20 31 20 3c /...SHA384 = 1 <
0790: 3c 20 31 38 2c 0a 09 09 2f 2a 20 53 48 41 2d 35 < 18,.../* SHA-5
07a0: 31 32 20 68 61 73 68 2e 20 2a 2f 0a 09 09 53 48 12 hash. */...SH
07b0: 41 35 31 32 20 3d 20 31 20 3c 3c 20 31 39 2c 0a A512 = 1 << 19,.
07c0: 09 09 2f 2a 20 45 44 4f 4e 2d 52 20 32 35 36 2e ../* EDON-R 256.
07d0: 20 2a 2f 0a 09 09 45 44 4f 4e 52 32 35 36 20 3d */...EDONR256 =
07e0: 20 31 20 3c 3c 20 32 30 2c 0a 09 09 2f 2a 20 45 1 << 20,.../* E
07f0: 44 4f 4e 2d 52 20 35 31 32 2e 20 2a 2f 0a 09 09 DON-R 512. */...
0800: 45 44 4f 4e 52 35 31 32 20 3d 20 31 20 3c 3c 20 EDONR512 = 1 <<
0810: 32 31 2c 0a 09 09 2f 2a 2a 20 53 48 41 33 2d 32 21,.../** SHA3-2
0820: 32 34 20 68 61 73 68 2e 20 2a 2f 0a 09 09 53 48 24 hash. */...SH
0830: 41 33 5f 32 32 34 20 3d 20 31 20 3c 3c 20 32 32 A3_224 = 1 << 22
0840: 2c 0a 09 09 2f 2a 2a 20 53 48 41 33 2d 32 35 36 ,.../** SHA3-256
0850: 20 68 61 73 68 2e 20 2a 2f 0a 09 09 53 48 41 33 hash. */...SHA3
0860: 5f 32 35 36 20 3d 20 31 20 3c 3c 20 32 33 2c 0a _256 = 1 << 23,.
0870: 09 09 2f 2a 2a 20 53 48 41 33 2d 33 38 34 20 68 ../** SHA3-384 h
0880: 61 73 68 2e 20 2a 2f 0a 09 09 53 48 41 33 5f 33 ash. */...SHA3_3
0890: 38 34 20 3d 20 31 20 3c 3c 20 32 34 2c 0a 09 09 84 = 1 << 24,...
08a0: 2f 2a 2a 20 53 48 41 33 2d 35 31 32 20 68 61 73 /** SHA3-512 has
08b0: 68 2e 20 2a 2f 0a 09 09 53 48 41 33 5f 35 31 32 h. */...SHA3_512
08c0: 20 3d 20 31 20 3c 3c 20 32 35 0a 09 7d 0a 7d 0a = 1 << 25..}.}.