Skip to content

Commit 012c2fc

Browse files
author
Jeen Broekstra
authored
Merge pull request #777 from Fedict/issues/#775-hash-zeros
Convert bytes to hex string with leading zeros
2 parents 38cfddf + 8f98fd0 commit 012c2fc

2 files changed

Lines changed: 43 additions & 11 deletions

File tree

  • core/queryalgebra/evaluation/src

core/queryalgebra/evaluation/src/main/java/org/eclipse/rdf4j/query/algebra/evaluation/function/hash/HashFunction.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
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

1010
import java.math.BigInteger;
@@ -19,24 +19,28 @@
1919

2020
/**
2121
* Abstract hash function
22-
*
22+
*
2323
* @author jeen
2424
*/
2525
public abstract class HashFunction implements Function {
2626

27+
/**
28+
* Calculate hash value, represented as hexadecimal string.
29+
*
30+
* @param text text
31+
* @param algorithm name of the hash algorithm
32+
* @return hexadecimal string (padded with leading zeros if needed)
33+
* @throws NoSuchAlgorithmException
34+
*/
2735
protected String hash(String text, String algorithm)
28-
throws NoSuchAlgorithmException
29-
{
36+
throws NoSuchAlgorithmException {
3037
byte[] hash = MessageDigest.getInstance(algorithm).digest(text.getBytes());
3138
BigInteger bi = new BigInteger(1, hash);
32-
String result = bi.toString(16);
33-
if (result.length() % 2 != 0) {
34-
return "0" + result;
35-
}
36-
return result;
39+
40+
return String.format("%0" + hash.length * 2 + "x", bi);
3741
}
3842

43+
@Override
3944
public abstract Literal evaluate(ValueFactory valueFactory, Value... args)
40-
throws ValueExprEvaluationException;
41-
45+
throws ValueExprEvaluationException;
4246
}
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)