Skip to content

fix: deal with the extra layer of hex #236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion sdk/src/main/java/io/opentdf/platform/sdk/TDF.java
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,9 @@ public TDFObject createTDF(InputStream payload,
assertion.appliesToState = assertionConfig.appliesToState.toString();

var assertionHashAsHex = assertion.hash();
var assertionHash = Hex.decodeHex(assertionHashAsHex);
var assertionHash = tdfConfig.hexEncodeRootAndSegmentHashes
? assertionHashAsHex.getBytes(StandardCharsets.UTF_8)
: Hex.decodeHex(assertionHashAsHex);
byte[] completeHash = new byte[aggregateHash.size() + assertionHash.length];
System.arraycopy(aggregateHash.toByteArray(), 0, completeHash, 0, aggregateHash.size());
System.arraycopy(assertionHash, 0, completeHash, aggregateHash.size(), assertionHash.length);
Expand Down
19 changes: 19 additions & 0 deletions sdk/src/test/java/io/opentdf/platform/sdk/TDFTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -500,11 +500,21 @@ public void testCreateTDFWithMimeType() throws Exception {
@Test
public void legacyTDFRoundTrips() throws DecoderException, IOException, ExecutionException, JOSEException, InterruptedException, ParseException, NoSuchAlgorithmException {
final String mimeType = "application/pdf";
var assertionConfig1 = new AssertionConfig();
assertionConfig1.id = "assertion1";
assertionConfig1.type = AssertionConfig.Type.BaseAssertion;
assertionConfig1.scope = AssertionConfig.Scope.TrustedDataObj;
assertionConfig1.appliesToState = AssertionConfig.AppliesToState.Unencrypted;
assertionConfig1.statement = new AssertionConfig.Statement();
assertionConfig1.statement.format = "base64binary";
assertionConfig1.statement.schema = "text";
assertionConfig1.statement.value = "ICAgIDxlZGoOkVkaD4=";

Config.TDFConfig config = Config.newTDFConfig(
Config.withAutoconfigure(false),
Config.withKasInformation(getRSAKASInfos()),
Config.withTargetMode("4.2.1"),
Config.withAssertionConfig(assertionConfig1),
Config.withMimeType(mimeType));

byte[] data = new byte[129];
Expand Down Expand Up @@ -537,6 +547,15 @@ public void legacyTDFRoundTrips() throws DecoderException, IOException, Executio
reader.readPayload(dataOutputStream);
assertThat(reader.getManifest().payload.mimeType).isEqualTo(mimeType);
assertArrayEquals(data, dataOutputStream.toByteArray(), "extracted data does not match");
var manifest = reader.getManifest();
var assertions = manifest.assertions;
assertThat(assertions.size()).isEqualTo(1);
var assertion = assertions.get(0);
assertThat(assertion.id).isEqualTo("assertion1");
assertThat(assertion.statement.format).isEqualTo("base64binary");
assertThat(assertion.statement.schema).isEqualTo("text");
assertThat(assertion.statement.value).isEqualTo("ICAgIDxlZGoOkVkaD4=");
assertThat(assertion.type).isEqualTo(AssertionConfig.Type.BaseAssertion.toString());
}

@Nonnull
Expand Down
Loading