Skip to content

Commit 114bbc8

Browse files
committed
Pull staff from java-interview-coding repo
1 parent ea1db07 commit 114bbc8

19 files changed

+1780
-0
lines changed

common/pom.xml

+35
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,41 @@
4141
<artifactId>activemq-broker</artifactId>
4242
<version>${activemq.version}</version>
4343
</dependency>
44+
45+
<!-- Staff for REST client. Selected versions are compatible for Java6u45 -->
46+
<dependency>
47+
<groupId>org.springframework</groupId>
48+
<artifactId>spring-web</artifactId>
49+
<version>4.1.9.RELEASE</version>
50+
</dependency>
51+
<dependency>
52+
<groupId>org.apache.httpcomponents</groupId>
53+
<artifactId>httpclient</artifactId>
54+
<version>4.5.14</version>
55+
</dependency>
56+
<dependency>
57+
<groupId>org.bouncycastle</groupId>
58+
<artifactId>bcprov-jdk15on</artifactId>
59+
<version>1.54</version>
60+
</dependency>
61+
<dependency>
62+
<groupId>com.fasterxml.jackson.core</groupId>
63+
<artifactId>jackson-databind</artifactId>
64+
<version>2.14.0</version>
65+
</dependency>
66+
67+
<dependency>
68+
<groupId>org.mockito</groupId>
69+
<artifactId>mockito-core</artifactId>
70+
<version>4.10.0</version>
71+
<scope>test</scope>
72+
</dependency>
73+
74+
<dependency>
75+
<groupId>org.jasypt</groupId>
76+
<artifactId>jasypt</artifactId>
77+
<version>1.9.3</version>
78+
</dependency>
4479
</dependencies>
4580

4681
<build>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package by.andd3dfx.digitalsignature;
2+
3+
import java.io.BufferedInputStream;
4+
import java.io.FileInputStream;
5+
import java.io.FileOutputStream;
6+
import java.io.IOException;
7+
import java.math.BigInteger;
8+
import java.security.*;
9+
import java.security.spec.PKCS8EncodedKeySpec;
10+
import java.security.spec.X509EncodedKeySpec;
11+
12+
public class DigitalSignatureUtil {
13+
14+
/**
15+
* According to documentation - https://docs.oracle.com/javase/8/docs/technotes/guides/security/StandardNames.html#KeyPairGenerator
16+
* possible cases for algorithm are next: DiffieHellman, DSA, RSA, EC
17+
*/
18+
public KeyPair generateKeysPair(String algorithm, int keysize) throws Exception {
19+
KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance(algorithm);
20+
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
21+
keyGenerator.initialize(keysize, random);
22+
return keyGenerator.generateKeyPair();
23+
}
24+
25+
/**
26+
* Store private & public keys from keys pair to files
27+
*/
28+
public void storeKeysToFiles(KeyPair keyPair, String privateKeyFilename, String publicKeyFilename) throws IOException {
29+
PrivateKey privateKey = keyPair.getPrivate();
30+
PublicKey publicKey = keyPair.getPublic();
31+
32+
saveToFile(privateKeyFilename, privateKey.getEncoded());
33+
saveToFile(publicKeyFilename, publicKey.getEncoded());
34+
}
35+
36+
private void saveToFile(String filename, byte[] data) throws IOException {
37+
FileOutputStream publicKeyFileOutputStream = new FileOutputStream(filename);
38+
publicKeyFileOutputStream.write(data);
39+
publicKeyFileOutputStream.close();
40+
}
41+
42+
private byte[] loadFromFile(String filename) throws IOException {
43+
FileInputStream fileInputStream = new FileInputStream(filename);
44+
byte[] result = new byte[fileInputStream.available()];
45+
fileInputStream.read(result);
46+
fileInputStream.close();
47+
return result;
48+
}
49+
50+
public PrivateKey loadPrivateKeyFromFile(String algorithm, String privateKeyFilename) throws Exception {
51+
byte[] encKey = loadFromFile(privateKeyFilename);
52+
PKCS8EncodedKeySpec encodedKeySpec = new PKCS8EncodedKeySpec(encKey);
53+
54+
KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
55+
return keyFactory.generatePrivate(encodedKeySpec);
56+
}
57+
58+
public PublicKey loadPublicKeyFromFile(String algorithm, String publicKeyFilename) throws Exception {
59+
byte[] encKey = loadFromFile(publicKeyFilename);
60+
X509EncodedKeySpec encodedKeySpec = new X509EncodedKeySpec(encKey);
61+
62+
KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
63+
return keyFactory.generatePublic(encodedKeySpec);
64+
}
65+
66+
/**
67+
* Sign string with definite algorithm and privateKey.
68+
* Usage: signString("SHA256withECDSA", privateKey, "Some string ...")
69+
*/
70+
public byte[] signString(String algorithm, PrivateKey privateKey, String stringTiSign) throws Exception {
71+
Signature dsa = Signature.getInstance(algorithm);
72+
dsa.initSign(privateKey);
73+
74+
byte[] strByte = stringTiSign.getBytes("UTF-8");
75+
dsa.update(strByte);
76+
77+
return dsa.sign();
78+
}
79+
80+
/**
81+
* Sign file with definite algorithm and privateKey.
82+
* Usage: signFile("SHA256withECDSA", privateKey, "d:/some-filename.txt")
83+
*/
84+
public byte[] signFile(String algorithm, PrivateKey privateKey, String nameOfFileToSign) throws Exception {
85+
Signature dsa = Signature.getInstance(algorithm);
86+
dsa.initSign(privateKey);
87+
88+
FileInputStream fis = new FileInputStream(nameOfFileToSign);
89+
BufferedInputStream bufferedInputStream = new BufferedInputStream(fis);
90+
byte[] buffer = new byte[1024];
91+
92+
int len;
93+
while ((len = bufferedInputStream.read(buffer)) >= 0) {
94+
dsa.update(buffer, 0, len);
95+
}
96+
bufferedInputStream.close();
97+
return dsa.sign();
98+
}
99+
100+
public String convertBytesArrayToString(byte[] signature) {
101+
return new BigInteger(1, signature).toString(16);
102+
}
103+
104+
public boolean verifyStringSignature(String algorithm, PublicKey publicKey, String signedString, byte[] signature) throws Exception {
105+
Signature sig = Signature.getInstance(algorithm);
106+
sig.initVerify(publicKey);
107+
sig.update(signedString.getBytes("UTF-8"));
108+
return sig.verify(signature);
109+
}
110+
111+
public boolean verifyFileSignature(String algorithm, PublicKey publicKey, String signedFileName, byte[] signatureToVerify) throws Exception {
112+
FileInputStream fileInputStream = new FileInputStream(signedFileName);
113+
byte[] bytes = new byte[fileInputStream.available()];
114+
fileInputStream.read(bytes);
115+
fileInputStream.close();
116+
117+
Signature sig = Signature.getInstance(algorithm);
118+
sig.initVerify(publicKey);
119+
sig.update(bytes);
120+
return sig.verify(signatureToVerify);
121+
}
122+
}

0 commit comments

Comments
 (0)