22
33import com .google .gson .Gson ;
44import com .google .gson .GsonBuilder ;
5- import com .nimbusds .jose .*;
6-
5+ import com .nimbusds .jose .JOSEException ;
76import io .opentdf .platform .policy .Value ;
87import io .opentdf .platform .policy .attributes .AttributesServiceGrpc .AttributesServiceFutureStub ;
9- import io .opentdf .platform .sdk .Config .TDFConfig ;
10- import io .opentdf .platform .sdk .Manifest .ManifestDeserializer ;
118import io .opentdf .platform .sdk .Autoconfigure .AttributeValueFQN ;
129import io .opentdf .platform .sdk .Config .KASInfo ;
13-
10+ import io .opentdf .platform .sdk .Config .TDFConfig ;
11+ import io .opentdf .platform .sdk .Manifest .ManifestDeserializer ;
1412import org .apache .commons .codec .DecoderException ;
1513import org .apache .commons .codec .binary .Hex ;
1614import org .erdtman .jcs .JsonCanonicalizer ;
1715import org .slf4j .Logger ;
1816import org .slf4j .LoggerFactory ;
1917
20- import java .io .BufferedReader ;
2118import java .io .ByteArrayOutputStream ;
2219import java .io .IOException ;
2320import java .io .InputStream ;
24- import java .io .InputStreamReader ;
2521import java .io .OutputStream ;
2622import java .nio .channels .SeekableByteChannel ;
2723import java .nio .charset .StandardCharsets ;
28- import java .security .*;
24+ import java .security .MessageDigest ;
25+ import java .security .NoSuchAlgorithmException ;
26+ import java .security .SecureRandom ;
2927import 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 ;
3138import 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