Skip to content
This repository was archived by the owner on Mar 15, 2021. It is now read-only.

Commit 023a10e

Browse files
authored
Merge pull request #6 from openregister/redaction
Add redaction support
2 parents e626bab + 92c4764 commit 023a10e

5 files changed

Lines changed: 55 additions & 14 deletions

File tree

‎build.gradle‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ dependencies {
1515
// This dependency is exported to consumers, that is to say found on their compile classpath.
1616
api 'org.apache.commons:commons-math3:3.6.1'
1717

18+
compile group: 'commons-codec', name: 'commons-codec', version: '1.11'
19+
1820
// This dependency is used internally, and not exposed to consumers on their own compile classpath.
1921
implementation 'com.google.guava:guava:23.0'
2022

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
#Fri Nov 23 10:30:05 GMT 2018
12
distributionBase=GRADLE_USER_HOME
23
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-bin.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip

‎src/main/java/uk/gov/objecthash/ObjectHashable.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package uk.gov.objecthash;
22

3+
import java.io.UnsupportedEncodingException;
34
import java.nio.charset.StandardCharsets;
45
import java.security.MessageDigest;
56
import java.security.NoSuchAlgorithmException;

‎src/main/java/uk/gov/objecthash/StringValue.java‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package uk.gov.objecthash;
22

3+
import org.apache.commons.codec.DecoderException;
4+
import org.apache.commons.codec.binary.Hex;
5+
36
public class StringValue implements ObjectHashable {
47
private final String value;
58

@@ -8,6 +11,21 @@ public StringValue(String value) {
811
}
912

1013
public byte[] digest() {
14+
if(value.startsWith("**REDACTED**")) {
15+
final String hexDigest = value.substring(12);
16+
try {
17+
return Hex.decodeHex(hexDigest);
18+
} catch (DecoderException e) {
19+
/*
20+
* If the redacted value is not valid hex, then treat it as
21+
* a non-redacted string. This ensures that every possible
22+
* data structure has a hash. We do not enforce that the hash
23+
* length matches any particular hashing algorithm, only that
24+
* it is hex encoded.
25+
*/
26+
}
27+
}
28+
1129
return ObjectHashable.digest(ObjectHashable.STRING_TAG, value);
1230
}
1331
}

‎src/test/java/uk/gov/objecthash/ObjectHashTest.java‎

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package uk.gov.objecthash;
2-
import static org.junit.Assert.assertEquals;
3-
import static org.junit.Assert.assertNotEquals;
2+
import org.junit.Test;
43

54
import java.nio.charset.StandardCharsets;
65
import java.time.Instant;
@@ -13,13 +12,14 @@
1312
import java.util.Map;
1413
import java.util.Set;
1514

16-
import org.junit.Test;
15+
import static org.junit.Assert.assertEquals;
16+
import static org.junit.Assert.assertNotEquals;
1717

1818

1919
public class ObjectHashTest {
20-
20+
2121
@Test
22-
public void hashesStrings() {
22+
public void hashesStrings() {
2323
assertEquals(ObjectHash.toHexDigest("Hello, Ruby!"), "c92765c1350e6df6800dbadedb3420f398d5f3c7e38a3da48aadb3332280f85f");
2424
}
2525

@@ -45,19 +45,19 @@ public void doesNotNormaliseByteArrays() {
4545

4646
@Test
4747
public void hashesIntegers() {
48-
assertEquals(ObjectHash.toHexDigest(-1), "f105b11df43d5d321f5c773ef904af979024887b4d2b0fab699387f59e2ff01e" );
49-
assertEquals(ObjectHash.toHexDigest(0), "a4e167a76a05add8a8654c169b07b0447a916035aef602df103e8ae0fe2ff390" );
50-
assertEquals(ObjectHash.toHexDigest(10), "73f6128db300f3751f2e509545be996d162d20f9e030864632f85e34fd0324ce" );
51-
assertEquals(ObjectHash.toHexDigest(1000), "a3346d18105ef801c3598fec426dcc5d4be9d0374da5343f6c8dcbdf24cb8e0b" );
52-
}
53-
48+
assertEquals(ObjectHash.toHexDigest(-1), "f105b11df43d5d321f5c773ef904af979024887b4d2b0fab699387f59e2ff01e");
49+
assertEquals(ObjectHash.toHexDigest(0), "a4e167a76a05add8a8654c169b07b0447a916035aef602df103e8ae0fe2ff390");
50+
assertEquals(ObjectHash.toHexDigest(10), "73f6128db300f3751f2e509545be996d162d20f9e030864632f85e34fd0324ce");
51+
assertEquals(ObjectHash.toHexDigest(1000), "a3346d18105ef801c3598fec426dcc5d4be9d0374da5343f6c8dcbdf24cb8e0b");
52+
}
53+
5454
@Test
5555
public void hashesTimestamps() {
5656
var date = LocalDate.parse("2000-01-01");
5757
Instant instant = date.atStartOfDay(ZoneId.of("UTC")).toInstant();
58-
assertEquals(ObjectHash.toHexDigest(instant), "cb34961d3d1a44386a73c37eb64c72a2b61ff40fc108abca92ef07c4954a1645" );
58+
assertEquals(ObjectHash.toHexDigest(instant), "cb34961d3d1a44386a73c37eb64c72a2b61ff40fc108abca92ef07c4954a1645");
5959
}
60-
60+
6161
@Test
6262
public void hashesDict() {
6363
Map<String, ObjectHashable> data = new HashMap<>();
@@ -136,4 +136,23 @@ public void hashesDictOfMixedTypes() {
136136

137137
assertEquals("45d9392ad17cead3fa46501eba3e5ac237cb46a39f1e175905f00ef6a6667257", digest);
138138
}
139+
140+
@Test
141+
public void returnsSuppliedHashIfRedacted() {
142+
StringValue original = new StringValue("hello");
143+
StringValue redacted = new StringValue("**REDACTED**" + original.hexDigest());
144+
145+
assertEquals(original.hexDigest(), redacted.hexDigest());
146+
}
147+
148+
@Test
149+
public void hashesKnownRedactedHash() {
150+
assertEquals(ObjectHash.toHexDigest("**REDACTED**2a42a9c91b74c0032f6b8000a2c9c5bcca5bb298f004e8eff533811004dea511"), "2a42a9c91b74c0032f6b8000a2c9c5bcca5bb298f004e8eff533811004dea511" );
151+
}
152+
153+
@Test
154+
public void hashesInvalidRedactedValues() {
155+
assertEquals(ObjectHash.toHexDigest("**REDACTED**banana"), "525b975c2979f427dcb79db8992035a9c2157a3c54492050753b9af552756c7a");
156+
}
157+
139158
}

0 commit comments

Comments
 (0)