Skip to content

Commit deda91e

Browse files
committed
Back to original algo + added test
Signed-off-by: Bart Hanssens <bart.hanssens@fedict.be>
1 parent 00823bf commit deda91e

2 files changed

Lines changed: 40 additions & 18 deletions

File tree

  • core/queryalgebra/evaluation/src
Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
/*******************************************************************************
1+
/** *****************************************************************************
22
* Copyright (c) 2015 Eclipse RDF4J contributors, Aduna, and others.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Distribution License v1.0
55
* which accompanies this distribution, and is available at
66
* http://www.eclipse.org/org/documents/edl-v10.php.
7-
*******************************************************************************/
7+
****************************************************************************** */
88
package org.eclipse.rdf4j.query.algebra.evaluation.function.hash;
99

10+
import java.math.BigInteger;
1011
import java.security.MessageDigest;
1112
import java.security.NoSuchAlgorithmException;
1213

@@ -18,35 +19,28 @@
1819

1920
/**
2021
* Abstract hash function
21-
*
22+
*
2223
* @author jeen
2324
*/
2425
public abstract class HashFunction implements Function {
25-
private final String HEX = "0123456789abcdef";
26-
26+
2727
/**
2828
* Calculate hash value, represented as hexadecimal string.
29-
*
29+
*
3030
* @param text text
3131
* @param algorithm name of the hash algorithm
32-
* @return hexadecimal string
33-
* @throws NoSuchAlgorithmException
32+
* @return hexadecimal string (padded with leading zeros if needed)
33+
* @throws NoSuchAlgorithmException
3434
*/
3535
protected String hash(String text, String algorithm)
36-
throws NoSuchAlgorithmException
37-
{
36+
throws NoSuchAlgorithmException {
3837
byte[] hash = MessageDigest.getInstance(algorithm).digest(text.getBytes());
38+
BigInteger bi = new BigInteger(1, hash);
3939

40-
// convert to hexadecimal representation, with leading zeros
41-
char[] hex = new char[hash.length * 2];
42-
for (int i = 0, j = 0; i < hex.length; ) {
43-
hex[i++] = HEX.charAt((hash[j] & 0xF0) >>> 4);
44-
hex[i++] = HEX.charAt(hash[j++] & 0x0F);
45-
}
46-
return new String(hex);
40+
return String.format("%0" + hash.length * 2 + "x", bi);
4741
}
4842

4943
@Override
5044
public abstract Literal evaluate(ValueFactory valueFactory, Value... args)
51-
throws ValueExprEvaluationException;
45+
throws ValueExprEvaluationException;
5246
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2017 Eclipse RDF4J contributors.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Distribution License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/org/documents/edl-v10.php.
7+
*******************************************************************************/
8+
package org.eclipse.rdf4j.query.algebra.evaluation.function.hash;
9+
10+
import org.junit.Before;
11+
12+
/**
13+
* @author Bart Hanssens
14+
*/
15+
public class HashLeadingZeroTest extends HashFunctionTest {
16+
/**
17+
* Test if hash function adds multiple leading zeros (if needed).
18+
* Test value and expected result provided by Vassil Momtchev..
19+
*
20+
* @throws java.lang.Exception
21+
*/
22+
@Before
23+
public void setUp() throws Exception {
24+
setHashFunction(new MD5());
25+
setToHash("363");
26+
setExpectedDigest("00411460f7c92d2124a67ea0f4cb5f85");
27+
}
28+
}

0 commit comments

Comments
 (0)