-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReleaseSubTaskEnvelope.java
More file actions
110 lines (91 loc) · 4.2 KB
/
ReleaseSubTaskEnvelope.java
File metadata and controls
110 lines (91 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import java.security.*;
import javax.crypto.*;
import java.util.ArrayList;
import java.util.Base64;
import java.nio.charset.StandardCharsets;
public class ReleaseSubTaskEnvelope extends Envelope {
private long timeForSubTaskCompletion;
private String encryptedFunctionType;
private String encryptedRangeStart;
private String encryptedRangeEnd;
public long getTimeForSubTaskCompletion() {
return timeForSubTaskCompletion;
}
public void setTimeForSubTaskCompletion(long timeForSubTaskCompletion) {
this.timeForSubTaskCompletion = timeForSubTaskCompletion;
}
public String getEncryptedFunctionType() {
return encryptedFunctionType;
}
public void setEncryptedFunctionType(String encryptedFunctionType) {
this.encryptedFunctionType = encryptedFunctionType;
}
public String getEncryptedRangeStart() {
return encryptedRangeStart;
}
public void setEncryptedRangeStart(String encryptedRangeStart) {
this.encryptedRangeStart = encryptedRangeStart;
}
public String getEncryptedRangeEnd() {
return encryptedRangeEnd;
}
public void setEncryptedRangeEnd(String encryptedRangeEnd) {
this.encryptedRangeEnd = encryptedRangeEnd;
}
private static byte[] generateSignature(PrivateKey secretKey, PublicKey publicKey, Envelope envelope) {
try {
// Obtain the encoded bytes of the secret key
byte[] secretKeyBytes = secretKey.getEncoded();
// Initialize the HMAC-SHA256 algorithm with the secret key
javax.crypto.Mac sha256_HMAC = javax.crypto.Mac.getInstance("HmacSHA256");
javax.crypto.spec.SecretKeySpec secretKeySpec = new javax.crypto.spec.SecretKeySpec(secretKeyBytes,
"HmacSHA256");
sha256_HMAC.init(secretKeySpec);
byte[] hash = sha256_HMAC.doFinal(envelope.toString().getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encode(hash);
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
public ReleaseSubTaskEnvelope(EnvelopeType envType, Node sentBy, Node receivedBy) {
super(envType, sentBy, receivedBy);
}
public ReleaseSubTaskEnvelope(EnvelopeType envType, Node sentBy, Node receivedBy, String functionType,
int rangeStart,
int rangeEnd, long timeForSubTaskCompletion, String encryptedFunctionType, String encryptedRangeStart,
String encryptedRangeEnd) {
super(envType, sentBy, receivedBy);
this.timeForSubTaskCompletion = timeForSubTaskCompletion;
this.encryptedFunctionType = encryptedFunctionType;
this.encryptedRangeStart = encryptedRangeStart;
this.encryptedRangeEnd = encryptedRangeEnd;
}
private static byte[] encryptWithPublicKey(byte[] data, PublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(data);
}
static Envelope createEnvelope(Node sender, Node receiver, String functionType, int rangeStart,
int rangeEnd, long time) {
ReleaseSubTaskEnvelope envelope = new ReleaseSubTaskEnvelope(EnvelopeType.envrv, sender, receiver);
envelope.setHashOfPrevEnvelope(null);
try {
// Encrypt functionType, rangeStart, and rangeEnd before creating the envelope
PublicKey publicKey = receiver.getPublicKey();
ArrayList<String> content = new ArrayList<>();
// content.add(functionType);
content.add(String.valueOf(rangeStart));
content.add(String.valueOf(rangeEnd));
byte[] encryptedContent = encryptWithPublicKey(content.toString().getBytes(StandardCharsets.UTF_8),
publicKey);
envelope.setEncryptedContent(Base64.getEncoder().encodeToString(encryptedContent));
String sign = Base64.getEncoder()
.encodeToString(generateSignature(sender.getPrivateKey(), publicKey, envelope));
envelope.setSign(sign);
} catch (Exception e) {
e.printStackTrace();
}
return envelope;
}
}