use of org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters in project popstellar by dedis.
the class Base64DataUtils method generatePoPToken.
/**
* @return a pseudo randomly generated valid Ed25519 keypair
*/
public static PoPToken generatePoPToken() {
byte[] privateKey = new byte[KEY_LENGTH];
RANDOM.nextBytes(privateKey);
Ed25519PrivateKeyParameters privateKeyParameters = new Ed25519PrivateKeyParameters(privateKey, 0);
Ed25519PublicKeyParameters publicKeyParameters = privateKeyParameters.generatePublicKey();
return new PoPToken(privateKey, publicKeyParameters.getEncoded());
}
use of org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters in project hedera-services by hashgraph.
the class Ed25519PrivateKey method fromBytes.
public static Ed25519PrivateKey fromBytes(byte[] keyBytes) {
Ed25519PrivateKeyParameters privKeyParams;
Ed25519PublicKeyParameters pubKeyParams = null;
if (keyBytes.length == Ed25519.SECRET_KEY_SIZE) {
// if the decoded bytes matches the length of a private key, try that
privKeyParams = new Ed25519PrivateKeyParameters(keyBytes, 0);
} else if (keyBytes.length == Ed25519.SECRET_KEY_SIZE + Ed25519.PUBLIC_KEY_SIZE) {
// some legacy code delivers private and public key pairs concatted together
try {
// this is how we read only the first 32 bytes
privKeyParams = new Ed25519PrivateKeyParameters(new ByteArrayInputStream(keyBytes, 0, Ed25519.SECRET_KEY_SIZE));
// read the remaining 32 bytes as the public key
pubKeyParams = new Ed25519PublicKeyParameters(keyBytes, Ed25519.SECRET_KEY_SIZE);
return new Ed25519PrivateKey(privKeyParams, pubKeyParams);
} catch (IOException e) {
throw new RuntimeException(e);
}
} else {
// decode a properly DER-encoded private key descriptor
var privateKeyInfo = PrivateKeyInfo.getInstance(keyBytes);
try {
var privateKey = privateKeyInfo.parsePrivateKey();
privKeyParams = new Ed25519PrivateKeyParameters(((ASN1OctetString) privateKey).getOctets(), 0);
var pubKeyData = privateKeyInfo.getPublicKeyData();
if (pubKeyData != null) {
pubKeyParams = new Ed25519PublicKeyParameters(pubKeyData.getOctets(), 0);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return new Ed25519PrivateKey(privKeyParams, pubKeyParams);
}
use of org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters in project hotmoka by Hotmoka.
the class ED25519 method encodingOf.
@Override
public byte[] encodingOf(PrivateKey privateKey) {
try {
PrivateKeyInfo privateKeyInfo = PrivateKeyInfo.getInstance(ASN1Primitive.fromByteArray(new PKCS8EncodedKeySpec(privateKey.getEncoded()).getEncoded()));
ASN1Encodable privateKey2 = privateKeyInfo.parsePrivateKey();
Ed25519PrivateKeyParameters privateKeyParams = new Ed25519PrivateKeyParameters(((ASN1OctetString) privateKey2).getOctets(), 0);
return privateKeyParams.getEncoded();
} catch (IOException e) {
throw InternalFailureException.of("cannot encode the private key", e);
}
}
use of org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters in project hotmoka by Hotmoka.
the class ED25519DET method encodingOf.
@Override
public byte[] encodingOf(PrivateKey privateKey) {
try {
PrivateKeyInfo privateKeyInfo = PrivateKeyInfo.getInstance(ASN1Primitive.fromByteArray(new PKCS8EncodedKeySpec(privateKey.getEncoded()).getEncoded()));
ASN1Encodable privateKey2 = privateKeyInfo.parsePrivateKey();
Ed25519PrivateKeyParameters privateKeyParams = new Ed25519PrivateKeyParameters(((ASN1OctetString) privateKey2).getOctets(), 0);
return privateKeyParams.getEncoded();
} catch (IOException e) {
throw InternalFailureException.of("cannot encode the private key", e);
}
}
use of org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters in project beckn-one by beckn.
the class SubscriberImpl method getRawPrivateKey.
public String getRawPrivateKey(String algo, String keyId) {
Subscriber subscriber = getProxy();
String pem = Request.getPrivateKey(subscriber.getSubscriberId(), keyId);
try {
if (!ObjectUtil.isVoid(pem)) {
PrivateKey privateKey = Crypt.getInstance().getPrivateKey(algo, pem);
if (privateKey instanceof BCEdDSAPrivateKey) {
BCEdDSAPrivateKey privateKey1 = (BCEdDSAPrivateKey) privateKey;
Field f = privateKey1.getClass().getDeclaredField("eddsaPrivateKey");
// BC Desnot expose this hence this reflection stuff.
f.setAccessible(true);
Ed25519PrivateKeyParameters privateKeyParameters1 = (Ed25519PrivateKeyParameters) f.get(privateKey1);
return Base64.getEncoder().encodeToString(privateKeyParameters1.getEncoded());
} else if (privateKey instanceof BCXDHPrivateKey) {
BCXDHPrivateKey privateKey1 = (BCXDHPrivateKey) privateKey;
Field f = privateKey1.getClass().getDeclaredField("xdhPrivateKey");
// BC Desnot expose this hence this reflection stuff.
f.setAccessible(true);
X25519PrivateKeyParameters privateKeyParameters1 = (X25519PrivateKeyParameters) f.get(privateKey1);
return Base64.getEncoder().encodeToString(privateKeyParameters1.getEncoded());
} else {
throw new RuntimeException("Key not identifiable!");
}
}
} catch (Exception ex) {
throw new RuntimeException(ex);
}
return null;
}
Aggregations