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
|
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;
/**
* Fragmento para firmar el mensaje con <strong>Clave privada PKCS #8 </strong>.
* <p><strong>Nota: </strong></p> Puede que sea necesario realizar cambios si su clave privada está en un formato diferente al de <strong>PKCS #8</strong>.
*/
public class ZenkiSnippet {
private static final String BEGIN_PRIVATE_KEY = "-----BEGIN PRIVATE KEY-----";
private static final String END_PRIVATE_KEY = "-----END PRIVATE KEY-----";
private static final String EMPTY_STRING = "";
private static final String BREAK_LINE = "\n";
/**
* Ejemplo de formato de clave privada base64 (<strong>PKCS #8</strong>).
**/
private static final String BASE_64_PRIVATE_KEY = "-----BEGIN PRIVATE KEY-----\n" +
"MIIJQgIBADANBgkqhkiG9w0BAQEFAASCCSwwggkoAgEAAoICAQCQ4h+g7C3Yu0XYF1JzrwG1Ob+qPVWeunrlufzhPGkRqf1vMXTkvwWEYrWdqaiUldOYejxmgLkO3g2ppBK+E/+2oS5qKM1kprssxEPJpxH4ZTS1xMmrsg/65HKIMYDLpixVUlX/GaTN+vrEulYfQtMFeEPQmJoysNZ+fjn21Flo09J2njIwLSarsryL0nvFvUc7GIMcRU35lcV5E5vpI45FWEoa6OJrgsMEioRi3tZHMxOKXXisj4aG2X0NsyBmd81/+jcbNZV3d5uPp8hbn9gBSEpR3a4hX+H7CHeGC+VjlO/rSiWrhd29O6mWnSYC7DV3JAGtRn/cCzq99X6Sg7uwpnS6cbUl4TOD4NaF3u/HcyfY1fu0AXeiyx6bIUASglHCjyWqL0r/SVOHKbOkHVDDmYq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Aec72TH7dwvGVKEMLB/5imJJ2qQowTSkQ+RV1ZSAY0vf69PJYg9noyUsMfgZAZB9dbTPWdiYLMueDuK7P+xGt2CfmjsUXUaiQ2X6qOD4kV9V3enxRmNKdLry0UTx0FJ0IcET9GjbucUsx8U7VbALVNsdH83Bs9NWGecSxsIsFYQ0FDEs8/fpgfrzdohrArMRW/TFN0JNdbDkOcZ8CggEAXnghKq+YHS/RRFC77ESSdxX+/Def8Zt+eOUIaEEGlA6KK+jG06yl8ipQCVoaKtdqngW9QpoSpt7SMPHehAES3xQC844o1ItbGk/IqK4jXMU2p+sjF6yUmzkPmAEnUkW3jh2UqYlFQNK2HA0baxCitRojv7ihADF1/+6MFwNwwd8B2ProNbwYvK2CHQkOBkhsjEEJllXhbVWXTzVyaatII0fKZj17ni04qp/UoDtp6+AB4sD87Ra7g03SSJQm2UtnRy+GoBZ/AcOFS3y4ii+SS3tFPthEXe9GF4s7ZI8iCJy1Hq/1pjMfa1ZesrUcHbkn2k0rI/xcLhScFHafYElS0w==\n" +
"-----END PRIVATE KEY-----";
/**
* Ruta de acceso al archivo de clave privada de muestra (<strong>PKCS #8</strong>).
**/
private static final String PRIVATE_KEY_FILE_PATH = "/path/to/my/file";
// Ejemplo de datos de compra JSON.
private static final String JSON_PURCHASE_DATA = "{\"version\":\"v1.1.0\",\"country\":\"US\",\"shopperCartId\":\"l93hyghr9hojqinx\",\"merchantOrderId\":\"l93hyghr4yty9jgm\",\"shopperEmail\":\"hello@zenki.fi\",\"purchaseSummary\":{\"currency\":\"USD\",\"totalItemsAmount\":\"0.5\",\"shipmentAmount\":\"0.25\",\"subtotalAmount\":\"0.75\",\"taxesAmount\":\"0.125\",\"localTaxesAmount\":\"0.125\",\"importCosts\":\"0.125\",\"discountAmount\":\"0.25\",\"additionalCharges\":{\"perquisite\":\"0.125\"},\"grandTotalAmount\":\"1\"},\"serviceType\":\"SERVICE\",\"items\":[{\"itemId\":\"l93hyghr5hp3lxcp\",\"price\":\"0.5\",\"quantity\":\"1\",\"productName\":\"Javascript\",\"thumbnailUrl\":\"https:\/\/tshirts.boutique\/wp-content\/uploads\/2022\/09\/12124-65.jpg\",\"metadata\":{\"size\":\"L\"}}],\"zenkipayKey\":\"e77e5b666253243e9179ff4d33fe364ec88aacf9240e5e7673e474255e4a900c\",\"metadata\":{\"anotherId\":\"l93hyghrcjs78929\"}}";
public static void main(String[] args) throws IOException, InvalidKeySpecException, NoSuchAlgorithmException, SignatureException, InvalidKeyException {
System.out.println("Signature: " + signMessageWithPkFromFile()); // Snippet to sing message getting private key from file.
System.out.println("Signature: " + signMessageWithPkBase64()); // Snippet to sing message getting private key from base64 string.
}
public static String signMessageWithPkFromFile() throws IOException, InvalidKeySpecException, NoSuchAlgorithmException, SignatureException, InvalidKeyException {
PrivateKey privateKey = getPKFromFile();
Signature signatureProvider = Signature.getInstance("SHA256withRSA");
signatureProvider.initSign(privateKey); // Private key
signatureProvider.update(JSON_PURCHASE_DATA.getBytes()); // Message to sign (in bytes)
byte[] signature = signatureProvider.sign(); // Signature
return Base64.getEncoder().encodeToString(signature); // Signature encoded in base64
}
public static String signMessageWithPkBase64() throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, SignatureException {
PrivateKey privateKey = getPKFromBase64String();
Signature signatureProvider = Signature.getInstance("SHA256withRSA");
signatureProvider.initSign(privateKey); // Private key
signatureProvider.update(JSON_PURCHASE_DATA.getBytes()); // Mensaje a firmar (en bytes)
byte[] signature = signatureProvider.sign(); // Signature
return Base64.getEncoder().encodeToString(signature); // Firma codificada en base64
}
private static PrivateKey getPKFromFile() throws IOException, InvalidKeySpecException, NoSuchAlgorithmException {
byte[] privateKey = Files.readAllBytes(Paths.get(PRIVATE_KEY_FILE_PATH)); // Getting file from filesystem
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKey, "RSA");
KeyFactory kf = KeyFactory.getInstance("RSA");
return kf.generatePrivate(keySpec);
}
private static PrivateKey getPKFromBase64String() throws NoSuchAlgorithmException, InvalidKeySpecException {
String formattedKeyString = removePrivateKeyHeaders(BASE_64_PRIVATE_KEY);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(formattedKeyString), "RSA");
KeyFactory kf = KeyFactory.getInstance("RSA");
return kf.generatePrivate(keySpec);
}
private static String removePrivateKeyHeaders(String keyString) {
return keyString
.replace(BREAK_LINE, EMPTY_STRING)
.replace(BEGIN_PRIVATE_KEY, EMPTY_STRING)
.replace(END_PRIVATE_KEY, EMPTY_STRING);
}
}
|