Skip to content

Commit

Permalink
Merge pull request #92 from UnionInternationalCheminsdeFer/consolidat…
Browse files Browse the repository at this point in the history
…e_signed_data_in_static_frame

split signature data handling between encode and decode
  • Loading branch information
CGantert345 authored Nov 25, 2024
2 parents 79c96b4 + 4e34615 commit fe15f69
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/uic/barcode/Decoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ public byte[] getEncodedLevel1Data() throws IOException, EncodingFormatException
if (!isStaticHeader(data) && dynamicFrame != null) {
return dynamicFrame.getLevel1DataBin();
} else if (staticFrame != null) {
return staticFrame.getDataForSignature();
return staticFrame.buildDataForSignature();
} else {
throw new EncodingFormatException("Unknown Header");
}
Expand All @@ -385,7 +385,7 @@ public byte[] getLevel1Signature() throws IOException, EncodingFormatException {
if (!isStaticHeader(data)) {
return dynamicFrame.getLevel2Data().getLevel1Signature();
} else if (staticFrame != null) {
return staticFrame.getDataForSignature();
return staticFrame.buildDataForSignature();
} else {
throw new EncodingFormatException("Unknown Header");
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/uic/barcode/Encoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ public byte[] getEncodedLevel1Data() throws IOException, EncodingFormatException
if (dynamicFrame != null) {
return DynamicFrameCoder.encodeLevel1(dynamicFrame);
} else if (staticFrame != null) {
return staticFrame.getDataForSignature();
return staticFrame.buildDataForSignature();
} else {
throw new EncodingFormatException("Unknown Header");
}
Expand Down
54 changes: 23 additions & 31 deletions src/main/java/org/uic/barcode/staticFrame/StaticFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,14 @@ public ArrayList<DataRecord> getDataRecords() {
* @throws IOException Signals that an I/O exception has occurred.
* @throws EncodingFormatException the encoding format exception
*/
public byte[] getDataForSignature() throws IOException, EncodingFormatException {
// data compression
if (signedData != null) return signedData;
public byte[] buildDataForSignature() throws IOException, EncodingFormatException {

if (signedData != null) {
//signed data already build
return signedData;
}

// data compression
Deflater deflater = new Deflater();
byte[] data = encodeData();
deflater.setInput(data);
Expand All @@ -310,7 +314,18 @@ public byte[] getDataForSignature() throws IOException, EncodingFormatException
}
compressStream.close();

return compressStream.toByteArray();
signedData = compressStream.toByteArray();

return signedData;
}

/**
* Gets the data for signing.
*
* @return the data to be signed
*/
public byte[] getDataForSignature() {
return signedData;
}

/**
Expand Down Expand Up @@ -620,30 +635,6 @@ private byte[] trimDsaSignature(byte[] sealdata) throws EncodingFormatException
}



/**
* Verify the signature
*
* Note: an appropriate security provider (e.g. BC) must be registered before
*
* @param key the key
* @param algo the algorithm name
* @return true, if successful
* @throws InvalidKeyException the invalid key exception
* @throws NoSuchAlgorithmException the no such algorithm exception
* @throws SignatureException the signature exception
* @throws IllegalArgumentException the illegal argument exception
* @throws UnsupportedOperationException the unsupported operation exception
* @throws EncodingFormatException
* @throws IOException
*/
public boolean ByAlgorithmName(PublicKey key, String algo) throws InvalidKeyException, NoSuchAlgorithmException, SignatureException, IllegalArgumentException, UnsupportedOperationException, IOException, EncodingFormatException {
Signature sig = Signature.getInstance(algo);
sig.initVerify(key);
sig.update(this.getDataForSignature());
return sig.verify(this.getSignature());
}

/**
* Verify the signature
*
Expand All @@ -659,6 +650,7 @@ public boolean ByAlgorithmName(PublicKey key, String algo) throws InvalidKeyExce
* @throws UnsupportedOperationException the unsupported operating exception
* @throws EncodingFormatException
* @throws IOException
* @deprecated
*/
public boolean verifyByAlgorithmOid(PublicKey key, String signingAlg) throws InvalidKeyException, NoSuchAlgorithmException, SignatureException, IllegalArgumentException, UnsupportedOperationException, IOException, EncodingFormatException {

Expand Down Expand Up @@ -778,7 +770,7 @@ public void signByAlgorithmOID(PrivateKey key,String signatureAlgorithmOid) thro
}
Signature sig = Signature.getInstance(algo);
sig.initSign(key);
signedData = getDataForSignature();
signedData = buildDataForSignature();
sig.update(signedData);
signature = sig.sign();
}
Expand Down Expand Up @@ -833,7 +825,7 @@ public void signByAlgorithmOID(PrivateKey key,String signatureAlgorithmOid, Prov
}

sig.initSign(key);
signedData = getDataForSignature();
signedData = buildDataForSignature();
sig.update(signedData);
signature = sig.sign();
}
Expand All @@ -857,7 +849,7 @@ public void signByAlgorithmOID(PrivateKey key,String signatureAlgorithmOid, Prov
public void signUsingAlgorithmName(PrivateKey key,String algo) throws NoSuchAlgorithmException, InvalidKeyException, SignatureException, IOException, EncodingFormatException {
Signature sig = Signature.getInstance(algo);
sig.initSign(key);
sig.update(getDataForSignature());
sig.update(buildDataForSignature());
signature = sig.sign();
}

Expand Down

0 comments on commit fe15f69

Please sign in to comment.