Skip to content

Commit 4a9e43d

Browse files
committed
refactor: Update test cases
1 parent cbeebf8 commit 4a9e43d

File tree

53 files changed

+1093
-343
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1093
-343
lines changed

src/main/java/com/danubetech/dataintegrity/canonicalizer/Canonicalizers.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
public class Canonicalizers {
99

10-
public static final JCSCanonicalizer CANONICALIZER_JCSCANONICALIZER = new JCSCanonicalizer();
10+
public static final JCSSHA256Canonicalizer CANONICALIZER_JCSCANONICALIZER = new JCSSHA256Canonicalizer();
1111
public static final URDNA2015Canonicalizer CANONICALIZER_URDNA2015CANONICALIZER = new URDNA2015Canonicalizer();
1212

1313
public static final List<? extends Canonicalizer> CANONICALIZERS = List.of(
Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
11
package com.danubetech.dataintegrity.canonicalizer;
22

33
import com.danubetech.dataintegrity.DataIntegrityProof;
4-
import com.danubetech.dataintegrity.util.SHAUtil;
54
import foundation.identity.jsonld.JsonLDException;
65
import foundation.identity.jsonld.JsonLDObject;
76
import org.erdtman.jcs.JsonCanonicalizer;
87

98
import java.io.IOException;
9+
import java.nio.charset.StandardCharsets;
1010
import java.security.GeneralSecurityException;
1111
import java.util.List;
1212

13-
public class JCSCanonicalizer extends Canonicalizer {
14-
15-
private static final JCSCanonicalizer INSTANCE = new JCSCanonicalizer();
13+
public abstract class JCSCanonicalizer extends Canonicalizer {
1614

1715
public JCSCanonicalizer() {
1816
super(List.of("jcs"));
1917
}
2018

21-
public static JCSCanonicalizer getInstance() {
22-
return INSTANCE;
23-
}
19+
public abstract int hashLength();
20+
public abstract byte[] hash(byte[] input) throws GeneralSecurityException;
2421

2522
@Override
2623
public String canonicalize(JsonLDObject jsonLDObject) throws JsonLDException, IOException {
@@ -31,30 +28,38 @@ public String canonicalize(JsonLDObject jsonLDObject) throws JsonLDException, IO
3128
@Override
3229
public byte[] canonicalize(DataIntegrityProof dataIntegrityProof, JsonLDObject jsonLdObject) throws IOException, GeneralSecurityException, JsonLDException {
3330

34-
// construct the LD proof without proof values
31+
// construct the LD object without proof
32+
33+
JsonLDObject jsonLdObjectWithoutProof = JsonLDObject.builder()
34+
.base(jsonLdObject)
35+
.build();
36+
DataIntegrityProof.removeFromJsonLdObject(jsonLdObjectWithoutProof);
37+
38+
// construct the LD proof options without proof values
3539

3640
DataIntegrityProof dataIntegrityProofWithoutProofValues = DataIntegrityProof.builder()
3741
.base(dataIntegrityProof)
3842
.defaultContexts(false)
3943
.build();
4044
DataIntegrityProof.removeLdProofValues(dataIntegrityProofWithoutProofValues);
4145

42-
// construct the LD object with proof without proof values
46+
// canonicalize the LD object and LD proof options
4347

44-
JsonLDObject jsonLdObjectWithProofWithoutProofValues = JsonLDObject.builder()
45-
.base(jsonLdObject)
46-
.build();
47-
jsonLdObjectWithProofWithoutProofValues.setDocumentLoader(jsonLdObject.getDocumentLoader());
48-
DataIntegrityProof.removeFromJsonLdObject(jsonLdObjectWithProofWithoutProofValues);
49-
dataIntegrityProofWithoutProofValues.addToJsonLDObject(jsonLdObjectWithProofWithoutProofValues);
48+
String canonicalizedJsonLdObjectWithoutProof = this.canonicalize(jsonLdObjectWithoutProof);
5049

51-
// canonicalize the LD object
50+
String canonicalizedLdProofWithoutProofValues = this.canonicalize(dataIntegrityProofWithoutProofValues);
5251

53-
String canonicalizedJsonLdObjectWithProofWithoutProofValues = this.canonicalize(jsonLdObjectWithProofWithoutProofValues);
52+
// hashing
53+
54+
byte[] canonicalizedJsonLdObjectWithoutProofHash = this.hash(canonicalizedJsonLdObjectWithoutProof.getBytes(StandardCharsets.UTF_8));
55+
byte[] canonicalizedLdProofWithoutProofValuesHash = this.hash(canonicalizedLdProofWithoutProofValues.getBytes(StandardCharsets.UTF_8));
5456

5557
// construct the canonicalization result
5658

57-
byte[] canonicalizationResult = SHAUtil.sha256(canonicalizedJsonLdObjectWithProofWithoutProofValues);
59+
byte[] canonicalizationResult = new byte[this.hashLength()*2];
60+
System.arraycopy(canonicalizedLdProofWithoutProofValuesHash, 0, canonicalizationResult, 0, this.hashLength());
61+
System.arraycopy(canonicalizedJsonLdObjectWithoutProofHash, 0, canonicalizationResult, this.hashLength(), this.hashLength());
62+
5863
return canonicalizationResult;
5964
}
6065
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.danubetech.dataintegrity.canonicalizer;
2+
3+
import com.danubetech.keyformats.crypto.provider.SHA256Provider;
4+
5+
import java.security.GeneralSecurityException;
6+
7+
public class JCSSHA256Canonicalizer extends JCSCanonicalizer {
8+
9+
private static final JCSSHA256Canonicalizer INSTANCE = new JCSSHA256Canonicalizer();
10+
11+
public static JCSSHA256Canonicalizer getInstance() {
12+
return INSTANCE;
13+
}
14+
15+
public int hashLength() {
16+
return 32;
17+
}
18+
19+
public byte[] hash(byte[] input) throws GeneralSecurityException {
20+
return SHA256Provider.get().sha256(input);
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.danubetech.dataintegrity.canonicalizer;
2+
3+
import com.danubetech.keyformats.crypto.provider.SHA384Provider;
4+
5+
import java.security.GeneralSecurityException;
6+
7+
public class JCSSHA384Canonicalizer extends JCSCanonicalizer {
8+
9+
private static final JCSSHA384Canonicalizer INSTANCE = new JCSSHA384Canonicalizer();
10+
11+
public static JCSSHA384Canonicalizer getInstance() {
12+
return INSTANCE;
13+
}
14+
15+
public int hashLength() {
16+
return 48;
17+
}
18+
19+
public byte[] hash(byte[] input) throws GeneralSecurityException {
20+
return SHA384Provider.get().sha384(input);
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.danubetech.dataintegrity.canonicalizer;
2+
3+
import com.danubetech.keyformats.crypto.provider.SHA512Provider;
4+
5+
import java.security.GeneralSecurityException;
6+
7+
public class JCSSHA512Canonicalizer extends JCSCanonicalizer {
8+
9+
private static final JCSSHA512Canonicalizer INSTANCE = new JCSSHA512Canonicalizer();
10+
11+
public static JCSSHA512Canonicalizer getInstance() {
12+
return INSTANCE;
13+
}
14+
15+
public int hashLength() {
16+
return 48;
17+
}
18+
19+
public byte[] hash(byte[] input) throws GeneralSecurityException {
20+
return SHA512Provider.get().sha512(input);
21+
}
22+
}

src/main/java/com/danubetech/dataintegrity/canonicalizer/RDFC10Canonicalizer.java

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
package com.danubetech.dataintegrity.canonicalizer;
22

3+
import com.apicatalog.jsonld.lang.Keywords;
34
import com.apicatalog.rdf.RdfDataset;
45
import com.apicatalog.rdf.RdfNQuad;
56
import com.apicatalog.rdf.canon.RdfCanonicalizer;
67
import com.apicatalog.rdf.io.nquad.NQuadsWriter;
78
import com.danubetech.dataintegrity.DataIntegrityProof;
8-
import com.danubetech.dataintegrity.util.SHAUtil;
99
import foundation.identity.jsonld.JsonLDException;
1010
import foundation.identity.jsonld.JsonLDObject;
11+
import foundation.identity.jsonld.JsonLDUtils;
1112

1213
import java.io.IOException;
1314
import java.io.StringWriter;
15+
import java.nio.charset.StandardCharsets;
1416
import java.security.GeneralSecurityException;
1517
import java.util.Collection;
1618
import java.util.List;
19+
import java.util.Objects;
1720

18-
public class RDFC10Canonicalizer extends Canonicalizer {
19-
20-
private static final RDFC10Canonicalizer INSTANCE = new RDFC10Canonicalizer();
21+
public abstract class RDFC10Canonicalizer extends Canonicalizer {
2122

2223
public RDFC10Canonicalizer() {
2324
super(List.of("RDFC-1.0"));
2425
}
2526

26-
public static RDFC10Canonicalizer getInstance() {
27-
return INSTANCE;
28-
}
27+
public abstract int hashLength();
28+
public abstract byte[] hash(byte[] input) throws GeneralSecurityException;
2929

3030
@Override
3131
public String canonicalize(JsonLDObject jsonLDObject) throws JsonLDException, IOException {
@@ -41,32 +41,38 @@ public String canonicalize(JsonLDObject jsonLDObject) throws JsonLDException, IO
4141
@Override
4242
public byte[] canonicalize(DataIntegrityProof dataIntegrityProof, JsonLDObject jsonLdObject) throws IOException, GeneralSecurityException, JsonLDException {
4343

44-
// construct the LD proof without proof values
44+
// construct the LD object without proof
45+
46+
JsonLDObject jsonLdObjectWithoutProof = JsonLDObject.builder()
47+
.base(jsonLdObject)
48+
.build();
49+
DataIntegrityProof.removeFromJsonLdObject(jsonLdObjectWithoutProof);
50+
51+
// construct the LD proof options without proof values
4552

4653
DataIntegrityProof dataIntegrityProofWithoutProofValues = DataIntegrityProof.builder()
4754
.base(dataIntegrityProof)
48-
.defaultContexts(true)
4955
.build();
5056
DataIntegrityProof.removeLdProofValues(dataIntegrityProofWithoutProofValues);
5157

52-
// construct the LD object without proof
58+
// canonicalize the LD object and LD proof options
5359

54-
JsonLDObject jsonLdObjectWithoutProof = JsonLDObject.builder()
55-
.base(jsonLdObject)
56-
.build();
5760
jsonLdObjectWithoutProof.setDocumentLoader(jsonLdObject.getDocumentLoader());
58-
DataIntegrityProof.removeFromJsonLdObject(jsonLdObjectWithoutProof);
59-
60-
// canonicalize the LD proof and LD object
61+
String canonicalizedJsonLdObjectWithoutProof = this.canonicalize(jsonLdObjectWithoutProof);
6162

63+
dataIntegrityProofWithoutProofValues.setDocumentLoader(jsonLdObject.getDocumentLoader());
6264
String canonicalizedLdProofWithoutProofValues = this.canonicalize(dataIntegrityProofWithoutProofValues);
63-
String canonicalizedJsonLdObjectWithoutProof = this.canonicalize(jsonLdObjectWithoutProof);
65+
66+
// hashing
67+
68+
byte[] canonicalizedJsonLdObjectWithoutProofHash = this.hash(canonicalizedJsonLdObjectWithoutProof.getBytes(StandardCharsets.UTF_8));
69+
byte[] canonicalizedLdProofWithoutProofValuesHash = this.hash(canonicalizedLdProofWithoutProofValues.getBytes(StandardCharsets.UTF_8));
6470

6571
// construct the canonicalization result
6672

67-
byte[] canonicalizationResult = new byte[64];
68-
System.arraycopy(SHAUtil.sha256(canonicalizedLdProofWithoutProofValues), 0, canonicalizationResult, 0, 32);
69-
System.arraycopy(SHAUtil.sha256(canonicalizedJsonLdObjectWithoutProof), 0, canonicalizationResult, 32, 32);
73+
byte[] canonicalizationResult = new byte[this.hashLength()*2];
74+
System.arraycopy(canonicalizedLdProofWithoutProofValuesHash, 0, canonicalizationResult, 0, this.hashLength());
75+
System.arraycopy(canonicalizedJsonLdObjectWithoutProofHash, 0, canonicalizationResult, this.hashLength(), this.hashLength());
7076

7177
return canonicalizationResult;
7278
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.danubetech.dataintegrity.canonicalizer;
2+
3+
import com.danubetech.keyformats.crypto.provider.SHA256Provider;
4+
5+
import java.security.GeneralSecurityException;
6+
7+
public class RDFC10SHA256Canonicalizer extends RDFC10Canonicalizer {
8+
9+
private static final RDFC10SHA256Canonicalizer INSTANCE = new RDFC10SHA256Canonicalizer();
10+
11+
public static RDFC10SHA256Canonicalizer getInstance() {
12+
return INSTANCE;
13+
}
14+
15+
@Override
16+
public int hashLength() {
17+
return 32;
18+
}
19+
20+
@Override
21+
public byte[] hash(byte[] input) throws GeneralSecurityException {
22+
return SHA256Provider.get().sha256(input);
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.danubetech.dataintegrity.canonicalizer;
2+
3+
import com.danubetech.keyformats.crypto.provider.SHA384Provider;
4+
5+
import java.security.GeneralSecurityException;
6+
7+
public class RDFC10SHA384Canonicalizer extends RDFC10Canonicalizer {
8+
9+
private static final RDFC10SHA384Canonicalizer INSTANCE = new RDFC10SHA384Canonicalizer();
10+
11+
public static RDFC10SHA384Canonicalizer getInstance() {
12+
return INSTANCE;
13+
}
14+
15+
@Override
16+
public int hashLength() {
17+
return 48;
18+
}
19+
20+
@Override
21+
public byte[] hash(byte[] input) throws GeneralSecurityException {
22+
return SHA384Provider.get().sha384(input);
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.danubetech.dataintegrity.canonicalizer;
2+
3+
import com.danubetech.keyformats.crypto.provider.SHA384Provider;
4+
5+
import java.security.GeneralSecurityException;
6+
7+
public class RDFC10SHA512Canonicalizer extends RDFC10Canonicalizer {
8+
9+
private static final RDFC10SHA512Canonicalizer INSTANCE = new RDFC10SHA512Canonicalizer();
10+
11+
public static RDFC10SHA512Canonicalizer getInstance() {
12+
return INSTANCE;
13+
}
14+
15+
@Override
16+
public int hashLength() {
17+
return 64;
18+
}
19+
20+
@Override
21+
public byte[] hash(byte[] input) throws GeneralSecurityException {
22+
return SHA384Provider.get().sha384(input);
23+
}
24+
}

src/main/java/com/danubetech/dataintegrity/canonicalizer/URDNA2015Canonicalizer.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,33 +40,41 @@ public String canonicalize(JsonLDObject jsonLDObject) throws JsonLDException, IO
4040
@Override
4141
public byte[] canonicalize(DataIntegrityProof dataIntegrityProof, JsonLDObject jsonLdObject) throws IOException, GeneralSecurityException, JsonLDException {
4242

43+
// construct the LD object without proof
44+
45+
JsonLDObject jsonLdObjectWithoutProof = JsonLDObject.builder()
46+
.base(jsonLdObject)
47+
.build();
48+
jsonLdObjectWithoutProof.setDocumentLoader(jsonLdObject.getDocumentLoader());
49+
DataIntegrityProof.removeFromJsonLdObject(jsonLdObjectWithoutProof);
50+
4351
// construct the LD proof without proof values
4452

4553
DataIntegrityProof dataIntegrityProofWithoutProofValues = DataIntegrityProof.builder()
4654
.base(dataIntegrityProof)
4755
.defaultContexts(false)
48-
.contexts(jsonLdObject.getContexts())
4956
.build();
57+
dataIntegrityProofWithoutProofValues.setDocumentLoader(jsonLdObject.getDocumentLoader());
5058
DataIntegrityProof.removeLdProofValues(dataIntegrityProofWithoutProofValues);
5159

52-
// construct the LD object without proof
60+
// canonicalize the LD object and LD proof options
5361

54-
JsonLDObject jsonLdObjectWithoutProof = JsonLDObject.builder()
55-
.base(jsonLdObject)
56-
.build();
5762
jsonLdObjectWithoutProof.setDocumentLoader(jsonLdObject.getDocumentLoader());
58-
DataIntegrityProof.removeFromJsonLdObject(jsonLdObjectWithoutProof);
59-
60-
// canonicalize the LD proof and LD object
63+
String canonicalizedJsonLdObjectWithoutProof = this.canonicalize(jsonLdObjectWithoutProof);
6164

65+
dataIntegrityProofWithoutProofValues.setDocumentLoader(jsonLdObject.getDocumentLoader());
6266
String canonicalizedLdProofWithoutProofValues = this.canonicalize(dataIntegrityProofWithoutProofValues);
63-
String canonicalizedJsonLdObjectWithoutProof = this.canonicalize(jsonLdObjectWithoutProof);
67+
68+
// hashing
69+
70+
byte[] canonicalizedJsonLdObjectWithoutProofHash = SHAUtil.sha256(canonicalizedJsonLdObjectWithoutProof);
71+
byte[] canonicalizedLdProofWithoutProofValuesHash = SHAUtil.sha256(canonicalizedLdProofWithoutProofValues);
6472

6573
// construct the canonicalization result
6674

6775
byte[] canonicalizationResult = new byte[64];
68-
System.arraycopy(SHAUtil.sha256(canonicalizedLdProofWithoutProofValues), 0, canonicalizationResult, 0, 32);
69-
System.arraycopy(SHAUtil.sha256(canonicalizedJsonLdObjectWithoutProof), 0, canonicalizationResult, 32, 32);
76+
System.arraycopy(canonicalizedLdProofWithoutProofValuesHash, 0, canonicalizationResult, 0, 32);
77+
System.arraycopy(canonicalizedJsonLdObjectWithoutProofHash, 0, canonicalizationResult, 32, 32);
7078

7179
return canonicalizationResult;
7280
}

0 commit comments

Comments
 (0)