forked from tronprotocol/java-tron
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSignUtils.java
More file actions
67 lines (60 loc) · 2.33 KB
/
SignUtils.java
File metadata and controls
67 lines (60 loc) · 2.33 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
package org.tron.common.crypto;
import java.security.SecureRandom;
import java.security.SignatureException;
import org.tron.common.crypto.ECKey.ECDSASignature;
import org.tron.common.crypto.sm2.SM2;
import org.tron.common.crypto.sm2.SM2.SM2Signature;
public class SignUtils {
public static SignInterface getGeneratedRandomSign(
SecureRandom secureRandom, boolean isECKeyCryptoEngine) {
if (isECKeyCryptoEngine) {
return new ECKey(secureRandom);
}
return new SM2(secureRandom);
}
public static SignInterface fromPrivate(byte[] privKeyBytes, boolean isECKeyCryptoEngine) {
if (isECKeyCryptoEngine) {
return ECKey.fromPrivate(privKeyBytes);
}
return SM2.fromPrivate(privKeyBytes);
}
public static byte[] signatureToAddress(
byte[] messageHash, String signatureBase64, boolean isECKeyCryptoEngine)
throws SignatureException {
try {
if (isECKeyCryptoEngine) {
return ECKey.signatureToAddress(messageHash, signatureBase64);
}
return SM2.signatureToAddress(messageHash, signatureBase64);
} catch (Exception e) {
throw new SignatureException(e);
}
}
public static SignatureInterface fromComponents(
byte[] r, byte[] s, byte v, boolean isECKeyCryptoEngine) {
if (isECKeyCryptoEngine) {
return ECKey.ECDSASignature.fromComponents(r, s, v);
}
return SM2.SM2Signature.fromComponents(r, s, v);
}
public static byte[] signatureToAddress(
byte[] messageHash, SignatureInterface signatureInterface, boolean isECKeyCryptoEngine)
throws SignatureException {
if (isECKeyCryptoEngine) {
if (!(signatureInterface instanceof ECDSASignature)) {
throw new IllegalArgumentException(
"Expected ECDSASignature for ECKey engine, got: "
+ (signatureInterface == null ? "null"
: signatureInterface.getClass().getName()));
}
return ECKey.signatureToAddress(messageHash, (ECDSASignature) signatureInterface);
}
if (!(signatureInterface instanceof SM2Signature)) {
throw new IllegalArgumentException(
"Expected SM2Signature for SM2 engine, got: "
+ (signatureInterface == null ? "null"
: signatureInterface.getClass().getName()));
}
return SM2.signatureToAddress(messageHash, (SM2Signature) signatureInterface);
}
}