Skip to content

Commit ef933c5

Browse files
committed
add a class that does all hex encoding
1 parent b075194 commit ef933c5

File tree

3 files changed

+41
-21
lines changed

3 files changed

+41
-21
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package io.opentdf.platform.sdk;
2+
3+
import org.apache.commons.codec.binary.Hex;
4+
5+
public class HexString {
6+
private final byte[] value;
7+
8+
public HexString(byte[] value) {
9+
this.value = value;
10+
}
11+
12+
String hexValue() {
13+
return Hex.encodeHexString(value);
14+
}
15+
}

sdk/src/main/java/io/opentdf/platform/sdk/Manifest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
import io.opentdf.platform.sdk.TDF.AssertionException;
1515

16-
import org.apache.commons.codec.binary.Hex;
1716
import org.erdtman.jcs.JsonCanonicalizer;
1817

1918
import java.io.IOException;
@@ -353,7 +352,7 @@ public String hash() throws IOException {
353352

354353
var assertionAsJson = gson.toJson(this);
355354
JsonCanonicalizer jc = new JsonCanonicalizer(assertionAsJson);
356-
return Hex.encodeHexString(digest.digest(jc.getEncodedUTF8()));
355+
return new HexString(digest.digest(jc.getEncodedUTF8())).hexValue();
357356
}
358357

359358
// Sign the assertion with the given hash and signature using the key.

sdk/src/main/java/io/opentdf/platform/sdk/TDF.java

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,39 @@
22

33
import com.google.gson.Gson;
44
import com.google.gson.GsonBuilder;
5-
import com.nimbusds.jose.*;
6-
5+
import com.nimbusds.jose.JOSEException;
76
import io.opentdf.platform.policy.Value;
87
import io.opentdf.platform.policy.attributes.AttributesServiceGrpc.AttributesServiceFutureStub;
9-
import io.opentdf.platform.sdk.Config.TDFConfig;
10-
import io.opentdf.platform.sdk.Manifest.ManifestDeserializer;
118
import io.opentdf.platform.sdk.Autoconfigure.AttributeValueFQN;
129
import io.opentdf.platform.sdk.Config.KASInfo;
13-
10+
import io.opentdf.platform.sdk.Config.TDFConfig;
11+
import io.opentdf.platform.sdk.Manifest.ManifestDeserializer;
1412
import org.apache.commons.codec.DecoderException;
1513
import org.apache.commons.codec.binary.Hex;
1614
import org.erdtman.jcs.JsonCanonicalizer;
1715
import org.slf4j.Logger;
1816
import org.slf4j.LoggerFactory;
1917

20-
import java.io.BufferedReader;
2118
import java.io.ByteArrayOutputStream;
2219
import java.io.IOException;
2320
import java.io.InputStream;
24-
import java.io.InputStreamReader;
2521
import java.io.OutputStream;
2622
import java.nio.channels.SeekableByteChannel;
2723
import java.nio.charset.StandardCharsets;
28-
import java.security.*;
24+
import java.security.MessageDigest;
25+
import java.security.NoSuchAlgorithmException;
26+
import java.security.SecureRandom;
2927
import java.text.ParseException;
30-
import java.util.*;
28+
import java.util.ArrayList;
29+
import java.util.Arrays;
30+
import java.util.Base64;
31+
import java.util.HashMap;
32+
import java.util.HashSet;
33+
import java.util.List;
34+
import java.util.Map;
35+
import java.util.Objects;
36+
import java.util.Set;
37+
import java.util.UUID;
3138
import java.util.concurrent.ExecutionException;
3239

3340
/**
@@ -268,11 +275,10 @@ private void prepareManifest(Config.TDFConfig tdfConfig, SDK.KAS kas) {
268275
symKeys.add(symKey);
269276

270277
// Add policyBinding
271-
var hexBinding = Hex.encodeHexString(
272-
CryptoUtils.CalculateSHA256Hmac(symKey, base64PolicyObject.getBytes(StandardCharsets.UTF_8)));
278+
var hexBinding = new HexString(CryptoUtils.CalculateSHA256Hmac(symKey, base64PolicyObject.getBytes(StandardCharsets.UTF_8)));
273279
var policyBinding = new Manifest.PolicyBinding();
274280
policyBinding.alg = kHmacIntegrityAlgorithm;
275-
policyBinding.hash = encoder.encodeToString(hexBinding.getBytes(StandardCharsets.UTF_8));
281+
policyBinding.hash = encoder.encodeToString(hexBinding.hexValue().getBytes(StandardCharsets.UTF_8));
276282

277283
// Add meta data
278284
var encryptedMetadata = new String();
@@ -383,8 +389,8 @@ public void readPayload(OutputStream outputStream) throws TDFReadFailed,
383389
outputStream.write(writeBuf);
384390

385391
} else {
386-
String segmentSig = Hex.encodeHexString(digest.digest(readBuf));
387-
if (segment.hash.compareTo(segmentSig) != 0) {
392+
var segmentSig = new HexString(digest.digest(readBuf));
393+
if (segment.hash.compareTo(segmentSig.hexValue()) != 0) {
388394
throw new SegmentSignatureMismatch("segment signature miss match");
389395
}
390396

@@ -401,15 +407,15 @@ public PolicyObject readPolicyObject() {
401407
private static String calculateSignature(byte[] data, byte[] secret, Config.IntegrityAlgorithm algorithm) {
402408
if (algorithm == Config.IntegrityAlgorithm.HS256) {
403409
byte[] hmac = CryptoUtils.CalculateSHA256Hmac(secret, data);
404-
return Hex.encodeHexString(hmac);
410+
return new HexString(hmac).hexValue();
405411
}
406412

407413
if (kGMACPayloadLength > data.length) {
408414
throw new FailedToCreateGMAC("fail to create gmac signature");
409415
}
410416

411417
byte[] gmacPayload = Arrays.copyOfRange(data, data.length - kGMACPayloadLength, data.length);
412-
return Hex.encodeHexString(gmacPayload);
418+
return new HexString(gmacPayload).hexValue();
413419
}
414420

415421
public TDFObject createTDF(InputStream payload,
@@ -718,11 +724,11 @@ public Reader loadTDF(SeekableByteChannel tdf, SDK.KAS kas,
718724
var hashValues = assertion.verify(assertionKey);
719725
var assertionAsJson = gson.toJson(assertion);
720726
JsonCanonicalizer jc = new JsonCanonicalizer(assertionAsJson);
721-
var hashOfAssertion = Hex.encodeHexString(digest.digest(jc.getEncodedUTF8()));
722-
var signature = aggregateHash + hashOfAssertion;
727+
var hashOfAssertion = new HexString(digest.digest(jc.getEncodedUTF8()));
728+
var signature = aggregateHash + hashOfAssertion.hexValue();
723729
var encodeSignature = Base64.getEncoder().encodeToString(signature.getBytes());
724730

725-
if (!Objects.equals(hashOfAssertion, hashValues.getAssertionHash())) {
731+
if (!Objects.equals(hashOfAssertion.hexValue(), hashValues.getAssertionHash())) {
726732
throw new AssertionException("assertion hash mismatch", assertion.id);
727733
}
728734

0 commit comments

Comments
 (0)