diff options
Diffstat (limited to 'Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/util/Hasher.java')
-rw-r--r-- | Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/util/Hasher.java | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/util/Hasher.java b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/util/Hasher.java new file mode 100644 index 00000000..0c5d7eba --- /dev/null +++ b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/util/Hasher.java | |||
@@ -0,0 +1,86 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2014, Miklos Foldenyi, Andras Szabolcs Nagy, Abel Hegedus, Akos Horvath, Zoltan Ujhelyi and Daniel Varro | ||
3 | * This program and the accompanying materials are made available under the | ||
4 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
5 | * http://www.eclipse.org/legal/epl-v20.html. | ||
6 | * | ||
7 | * SPDX-License-Identifier: EPL-2.0 | ||
8 | *******************************************************************************/ | ||
9 | package org.eclipse.viatra.dse.util; | ||
10 | |||
11 | import java.math.BigInteger; | ||
12 | import java.security.MessageDigest; | ||
13 | import java.security.NoSuchAlgorithmException; | ||
14 | |||
15 | import org.eclipse.viatra.dse.api.DSEException; | ||
16 | |||
17 | /** | ||
18 | * Utility class that encapsulates a {@link MessageDigest} instance to aid calculating hash values more easily, and to | ||
19 | * reuse a {@link MessageDigest} instance. | ||
20 | * | ||
21 | */ | ||
22 | public final class Hasher { | ||
23 | private MessageDigest md; | ||
24 | |||
25 | private static final int HEX = 16; | ||
26 | |||
27 | private Hasher(MessageDigest md) { | ||
28 | this.md = md; | ||
29 | } | ||
30 | |||
31 | /** | ||
32 | * Calculates and returns a hash value. | ||
33 | * | ||
34 | * @param data | ||
35 | * the data to be hashed in a {@link String}. | ||
36 | * @return the hash value in some {@link String} representation. | ||
37 | */ | ||
38 | public String hash(String data) { | ||
39 | md.update(data.getBytes(), 0, data.length()); | ||
40 | return new String(md.digest()); | ||
41 | } | ||
42 | |||
43 | @SuppressWarnings("unused") | ||
44 | private String alternateHashBest(String data) { | ||
45 | md.update(data.getBytes(), 0, data.length()); | ||
46 | return new String(md.digest()); | ||
47 | } | ||
48 | |||
49 | @SuppressWarnings("unused") | ||
50 | private String alternateHashSecondBest(String data) { | ||
51 | md.update(data.getBytes(), 0, data.length()); | ||
52 | return new BigInteger(1, md.digest()).toString(HEX); | ||
53 | } | ||
54 | |||
55 | @SuppressWarnings("unused") | ||
56 | private String alternateHashThirdBest(String data) { | ||
57 | md.update(data.getBytes(), 0, data.length()); | ||
58 | byte[] array = md.digest(); | ||
59 | StringBuilder sb = new StringBuilder(); | ||
60 | for (int i = 0; i < array.length; i++) { | ||
61 | sb.append(Integer.toHexString((int) array[i])); | ||
62 | } | ||
63 | return sb.toString(); | ||
64 | } | ||
65 | |||
66 | /** | ||
67 | * Returns a {@link Hasher} with an internal {@link MessageDigest} that is based on the protocol named | ||
68 | * {@code protocoll}. | ||
69 | * | ||
70 | * @param protocoll | ||
71 | * the name of the hash algorythm. | ||
72 | * @return the initialized {@link Hasher} | ||
73 | * | ||
74 | * @throws DSEException | ||
75 | * on initialization failure. | ||
76 | */ | ||
77 | public static Hasher getHasher(String protocoll) { | ||
78 | try { | ||
79 | return new Hasher(MessageDigest.getInstance(protocoll)); | ||
80 | } catch (NoSuchAlgorithmException e) { | ||
81 | throw new DSEException(e); | ||
82 | } | ||
83 | } | ||
84 | |||
85 | public static final String SHA1_PROTOCOLL = "SHA-1"; | ||
86 | } | ||