io.takamaka.wallet.TkmCypherProviderBCED25519 Maven / Gradle / Ivy
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package io.takamaka.wallet;
import io.takamaka.wallet.beans.TkmCypherBean;
import io.takamaka.wallet.exceptions.KeyDecodeException;
import io.takamaka.wallet.utils.TkmSignUtils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters;
import org.bouncycastle.crypto.params.Ed25519PublicKeyParameters;
import org.bouncycastle.crypto.signers.Ed25519Signer;
import org.bouncycastle.util.Strings;
import org.bouncycastle.util.encoders.UrlBase64;
/**
*
* @author Giovanni Antino [email protected]
*/
@Slf4j
public class TkmCypherProviderBCED25519{
/**
*
* @param keyPair is the public and private key generated by using the sign key index
* @param message is the json format of the Internal Transaction Bean both with a generated random seed and the wallet cypher name
* Message content: : tb.getMessage() + tb.getRandomSeed() + tb.getWalletCypher().name()
* @return TkmCypherBean - Java Bean Class containing the generated signature
*/
public static TkmCypherBean sign(AsymmetricCipherKeyPair keyPair, String message) {
TkmCypherBean tcb = new TkmCypherBean();
//F.y(message);
tcb.setValid(false);
try {
byte[] byteMessage = Strings.toByteArray(message);
Ed25519Signer signer = new Ed25519Signer();
signer.reset();
signer.init(true, (Ed25519PrivateKeyParameters) keyPair.getPrivate());
signer.update(byteMessage, 0, byteMessage.length);
byte[] generatedSignature = signer.generateSignature();
//UrlBase64 b64e = new UrlBase64();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
UrlBase64.encode(generatedSignature, baos);
tcb.setSignature(baos.toString());
tcb.setValid(true);
baos.close();
} catch (IOException ex) {
tcb.setEx(ex);
tcb.setValid(false);
log.error("Error signing message.", ex);
}
return tcb;
}
/**
* @param keyPair is the public and private key generated by using the sign key index
* @param signature
* @param message is the json format of the Internal Transaction Bean both with a generated random seed and the wallet cypher name
* Message content: : tb.getMessage() + tb.getRandomSeed() + tb.getWalletCypher().name()
* @return TkmCypherBean
*/
public static TkmCypherBean verify(AsymmetricCipherKeyPair keyPair, String signature, String message) {
TkmCypherBean tcb = new TkmCypherBean();
tcb.setValid(false);
try {
//boolean isValidSignature = false;
//UrlBase64 b64e = new UrlBase64();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
UrlBase64.decode(signature, baos);
byte[] signatureByteArray = baos.toByteArray();
byte[] messageByteArray = Strings.toByteArray(message);
Ed25519Signer verifier = new Ed25519Signer();
verifier.reset();
Ed25519PublicKeyParameters edPublicKey = (Ed25519PublicKeyParameters) keyPair.getPublic();
verifier.init(false, edPublicKey);
verifier.update(messageByteArray, 0, messageByteArray.length);
tcb.setValid(verifier.verifySignature(signatureByteArray));
baos.close();
} catch (IOException ex) {
tcb.setEx(ex);
tcb.setValid(false);
log.error("Error verifying signature.", ex);
}
return tcb;
}
/**
*
* @param publicKey
* @param signature
* @param message is the json format of the Internal Transaction Bean both with a generated random seed and the wallet cypher name
* Message content: : tb.getMessage() + tb.getRandomSeed() + tb.getWalletCypher().name()
* @return TkmCypherBean
*/
public static TkmCypherBean verify(String publicKey, String signature, String message) {
try {
return TkmCypherProviderBCED25519.verify(TkmSignUtils.stringPublicKeyToKeyPairBCEd25519(publicKey), signature, message);
} catch (KeyDecodeException ex) {
TkmCypherBean tcb = new TkmCypherBean();
tcb.setEx(ex);
tcb.setValid(false);
log.error("Error verifying signature.", ex);
return tcb;
}
}
}