| 5 | import java.util.Random; |
| 6 | |
| 7 | public class Hash { |
| 8 | |
| 9 | private static Random random = new Random(); |
| 10 | |
| 11 | public static void setSeed(long seed) { |
| 12 | random.setSeed(seed); |
| 13 | } |
| 14 | |
| 15 | public static long hash64(long x, long seed) { |
| 16 | x += seed; |
| 17 | x = (x ^ (x >>> 33)) * 0xff51afd7ed558ccdL; |
| 18 | x = (x ^ (x >>> 33)) * 0xc4ceb9fe1a85ec53L; |
| 19 | x = x ^ (x >>> 33); |
| 20 | return x; |
| 21 | } |
| 22 | |
| 23 | public static long randomSeed() { |
| 24 | return random.nextLong(); |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Shrink the hash to a value 0..n. Kind of like modulo, but using |
| 29 | * multiplication and shift, which are faster to compute. |
| 30 | * |
| 31 | * @param hash the hash |
| 32 | * @param n the maximum of the result |
| 33 | * @return the reduced value |
| 34 | */ |
| 35 | public static int reduce(int hash, int n) { |
| 36 | // http://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/ |
| 37 | return (int) (((hash & 0xffffffffL) * n) >>> 32); |
| 38 | } |
| 39 | |
| 40 | public static byte[] Get_SHA_256(byte[] passwordToHash) { |
| 41 | try { |
| 42 | MessageDigest md = MessageDigest.getInstance("Sha-256"); |
| 43 | md.update(passwordToHash); |
| 44 | byte[] bytes = md.digest(); |
| 45 | return bytes; |
| 46 | } |
| 47 | catch (NoSuchAlgorithmException e) { |
| 48 | e.printStackTrace(); |
| 49 | } |
| 50 | return null; |
| 51 | } |
| 52 | |
| 53 | public static byte[] Get_SHA_128(byte[] passwordToHash) { |
| 54 | try { |
| 55 | MessageDigest md = MessageDigest.getInstance("Sha-256"); |
| 56 | md.update(passwordToHash); |
| 57 | byte[] bytes = md.digest(); |
| 58 | byte[] hash_128 = new byte[16]; |
| 59 | System.arraycopy(bytes,0,hash_128,0,16); |
| 60 | return hash_128; |
| 61 | } |
| 62 | catch (NoSuchAlgorithmException e) { |
| 63 | e.printStackTrace(); |
| 64 | } |
nothing calls this directly
no outgoing calls
no test coverage detected