diff options
author | CGantert345 <57003061+CGantert345@users.noreply.github.com> | 2022-11-22 14:04:27 +0100 |
---|---|---|
committer | CGantert345 <57003061+CGantert345@users.noreply.github.com> | 2022-11-22 14:04:27 +0100 |
commit | fc6a4042f7fca1828f0b8e267cfd660e6fe1d40a (patch) | |
tree | 8547d80b7536becc8fdcc1b42b4c2a9a8bb1d7c6 /src/main/java/org/uic/barcode/utils | |
parent | Merge pull request #54 from UnionInternationalCheminsdeFer/adding_comments_on_the_coutry_code_of_Kosovo (diff) | |
download | UIC-barcode-fc6a4042f7fca1828f0b8e267cfd660e6fe1d40a.tar UIC-barcode-fc6a4042f7fca1828f0b8e267cfd660e6fe1d40a.tar.gz UIC-barcode-fc6a4042f7fca1828f0b8e267cfd660e6fe1d40a.tar.bz2 UIC-barcode-fc6a4042f7fca1828f0b8e267cfd660e6fe1d40a.tar.lz UIC-barcode-fc6a4042f7fca1828f0b8e267cfd660e6fe1d40a.tar.xz UIC-barcode-fc6a4042f7fca1828f0b8e267cfd660e6fe1d40a.tar.zst UIC-barcode-fc6a4042f7fca1828f0b8e267cfd660e6fe1d40a.zip |
Diffstat (limited to 'src/main/java/org/uic/barcode/utils')
-rw-r--r-- | src/main/java/org/uic/barcode/utils/SecurityUtils.java | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/src/main/java/org/uic/barcode/utils/SecurityUtils.java b/src/main/java/org/uic/barcode/utils/SecurityUtils.java index 5fdbda7..29a2346 100644 --- a/src/main/java/org/uic/barcode/utils/SecurityUtils.java +++ b/src/main/java/org/uic/barcode/utils/SecurityUtils.java @@ -1,5 +1,8 @@ package org.uic.barcode.utils;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
@@ -10,6 +13,7 @@ import java.security.Security; import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
+import java.util.Arrays;
/**
* The Class SecurityUtils.
@@ -182,4 +186,81 @@ public class SecurityUtils { return null;
}
+
+ /**
+ * Decode signature integer sequence.
+ *
+ * Support function to decode a DSA signature
+ * Provides the two DSA signature parameter encoded in a DSA signature
+ *
+ * @param bytes the bytes
+ * @return the big integer[]
+ * @throws Exception the exception
+ */
+ public static BigInteger[] decodeSignatureIntegerSequence(byte[] bytes) throws Exception {
+
+ int sequenceTag = (int) bytes[0];
+
+ if (sequenceTag != 48) throw new Exception("signature is not a sequence");
+
+ int sequenceLength = (int) bytes[1];
+
+ if (sequenceLength < 6) throw new Exception("signature sequence too short");
+
+ BigInteger[] result = new BigInteger[2];
+
+ int offset = 2;
+ int i = 0;
+ while (offset < bytes.length && i < 2) {
+ int integerTag = (int) bytes[offset];
+ if (integerTag != 2) throw new Exception("signature is not an integer sequence");
+ int integerLength = (int) bytes[offset + 1];
+ byte[] value = Arrays.copyOfRange(bytes, offset + 2, offset + 2 + integerLength);
+ result[i] = new BigInteger(+1, value);
+ offset = offset + integerLength + 2;
+ i++;
+ }
+
+ return result;
+ }
+
+ /**
+ * Encode signature integer sequence.
+ *
+ * Support function to format two parameters as DER encoded integer list
+ * to get a valid formated DSA signature from the signature parameter
+ *
+ * @param i1 the i 1
+ * @param i2 the i 2
+ * @return the byte[]
+ * @throws IOException Signals that an I/O exception has occurred.
+ */
+ public static byte[] encodeSignatureIntegerSequence(BigInteger i1, BigInteger i2) throws IOException {
+
+ //SEQUENCE OF --> tag 16
+ int sequenceTag = 16 + 32; // (bits 6 = 1 constructed)
+ //INTEGER --> tag 2
+ int integerTag = 2;
+
+ byte[] b1 = i1.toByteArray();
+ int lb1 = b1.length;
+ byte[] b2 = i2.toByteArray();
+ int lb2 = b2.length;
+
+ int sequenceLength = lb1 + lb2 + 4;
+
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+
+ out.write((byte) sequenceTag);
+ out.write((byte) sequenceLength);
+ out.write((byte) integerTag);
+ out.write((byte) lb1);
+ out.write(b1);
+ out.write((byte) integerTag);
+ out.write((byte) lb2);
+ out.write(b2);
+
+ return out.toByteArray();
+ }
+
}
|