org.bouncycastle.jcajce.provider.asymmetric.gost.GOSTUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bcprov-jdk15to18 Show documentation
Show all versions of bcprov-jdk15to18 Show documentation
The Bouncy Castle Crypto package is a Java implementation of cryptographic algorithms. This jar contains JCE provider and lightweight API for the Bouncy Castle Cryptography APIs for JDK 1.5 to JDK 1.8.
package org.bouncycastle.jcajce.provider.asymmetric.gost;
import java.math.BigInteger;
import org.bouncycastle.crypto.params.GOST3410Parameters;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.Fingerprint;
import org.bouncycastle.util.Strings;
class GOSTUtil
{
static String privateKeyToString(String algorithm, BigInteger x, GOST3410Parameters gostParams)
{
StringBuffer buf = new StringBuffer();
String nl = Strings.lineSeparator();
BigInteger y = gostParams.getA().modPow(x, gostParams.getP());
buf.append(algorithm);
buf.append(" Private Key [").append(generateKeyFingerprint(y, gostParams)).append("]").append(nl);
buf.append(" Y: ").append(y.toString(16)).append(nl);
return buf.toString();
}
static String publicKeyToString(String algorithm, BigInteger y, GOST3410Parameters gostParams)
{
StringBuffer buf = new StringBuffer();
String nl = Strings.lineSeparator();
buf.append(algorithm);
buf.append(" Public Key [").append(generateKeyFingerprint(y, gostParams)).append("]").append(nl);
buf.append(" Y: ").append(y.toString(16)).append(nl);
return buf.toString();
}
private static String generateKeyFingerprint(BigInteger y, GOST3410Parameters dhParams)
{
return new Fingerprint(
Arrays.concatenate(
y.toByteArray(),
dhParams.getP().toByteArray(), dhParams.getA().toByteArray())).toString();
}
}