use of java.security.spec.ECPoint in project spring-security-oauth by spring-projects.
the class EllipticCurveKeyHelper method createPublicKey.
static ECPublicKey createPublicKey(final BigInteger x, final BigInteger y, final String curve) {
ECNamedCurveParameterSpec curveParameterSpec;
if ((curveParameterSpec = ECNamedCurveTable.getParameterSpec(curve)) == null) {
throw new IllegalArgumentException("Unsupported named curve: " + curve);
}
ECParameterSpec parameterSpec = new ECNamedCurveSpec(curveParameterSpec.getName(), curveParameterSpec.getCurve(), curveParameterSpec.getG(), curveParameterSpec.getN());
ECPublicKeySpec publicKeySpec = new ECPublicKeySpec(new ECPoint(x, y), parameterSpec);
try {
return (ECPublicKey) KeyFactory.getInstance("EC").generatePublic(publicKeySpec);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
use of java.security.spec.ECPoint in project wycheproof by google.
the class EcdsaTest method testBasic.
/**
* This test checks the basic functionality of ECDSA. It can also be used to generate simple test
* vectors.
*/
public void testBasic() throws Exception {
String algorithm = "SHA256WithECDSA";
String hashAlgorithm = "SHA-256";
String message = "Hello";
String curve = "secp256r1";
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");
ECGenParameterSpec ecSpec = new ECGenParameterSpec("secp256r1");
keyGen.initialize(ecSpec);
KeyPair keyPair = keyGen.generateKeyPair();
ECPublicKey pub = (ECPublicKey) keyPair.getPublic();
ECPrivateKey priv = (ECPrivateKey) keyPair.getPrivate();
byte[] messageBytes = message.getBytes("UTF-8");
Signature signer = Signature.getInstance(algorithm);
Signature verifier = Signature.getInstance(algorithm);
signer.initSign(priv);
signer.update(messageBytes);
byte[] signature = signer.sign();
verifier.initVerify(pub);
verifier.update(messageBytes);
assertTrue(verifier.verify(signature));
// Extract some parameters.
byte[] rawHash = MessageDigest.getInstance(hashAlgorithm).digest(messageBytes);
ECParameterSpec params = priv.getParams();
// Print keys and signature, so that it can be used to generate new test vectors.
System.out.println("Message:" + message);
System.out.println("Hash:" + TestUtil.bytesToHex(rawHash));
System.out.println("Curve:" + curve);
System.out.println("Order:" + params.getOrder().toString());
System.out.println("Private key:");
System.out.println("S:" + priv.getS().toString());
System.out.println("encoded:" + TestUtil.bytesToHex(priv.getEncoded()));
System.out.println("Public key:");
ECPoint w = pub.getW();
System.out.println("X:" + w.getAffineX().toString());
System.out.println("Y:" + w.getAffineY().toString());
System.out.println("encoded:" + TestUtil.bytesToHex(pub.getEncoded()));
System.out.println("Signature:" + TestUtil.bytesToHex(signature));
System.out.println("r:" + extractR(signature).toString());
System.out.println("s:" + extractS(signature).toString());
}
use of java.security.spec.ECPoint in project robovm by robovm.
the class ECPublicKeySpecTest method setUp.
protected void setUp() throws Exception {
super.setUp();
ECPoint ecpoint = new ECPoint(BigInteger.valueOf(1), BigInteger.valueOf(1));
EllipticCurve curve = new EllipticCurve(new ECFieldF2m(2), BigInteger.valueOf(1), BigInteger.valueOf(1));
w = new ECPoint(BigInteger.valueOf(1), BigInteger.valueOf(1));
params = new ECParameterSpec(curve, ecpoint, BigInteger.valueOf(1), 1);
ecpks = new ECPublicKeySpec(w, params);
}
use of java.security.spec.ECPoint in project robovm by robovm.
the class ECParameterSpecTest method setUp.
protected void setUp() throws Exception {
super.setUp();
curve = new EllipticCurve(new ECFieldF2m(2), BigInteger.valueOf(1), BigInteger.valueOf(1));
ecpoint = new ECPoint(BigInteger.valueOf(1), BigInteger.valueOf(1));
ecps = new ECParameterSpec(curve, ecpoint, BigInteger.valueOf(1), 1);
}
use of java.security.spec.ECPoint in project robovm by robovm.
the class ECPointTest method testEqualsObject02.
/**
* Test #2 for <code>equals(Object other)</code> method<br>
* Assertion: return false if this and other objects are not equal<br>
* Test preconditions: see test comments<br>
* Expected: all objects in this test must be not equal
*/
public final void testEqualsObject02() {
// test case 1: must be not equal to null
ECPoint p2 = null, p1 = new ECPoint(BigInteger.valueOf(-23456L), BigInteger.ONE);
assertFalse(p1.equals(p2));
// test case 2: not equal objects - x
p1 = new ECPoint(BigInteger.valueOf(-23457L), BigInteger.ONE);
p2 = new ECPoint(BigInteger.valueOf(-23456L), BigInteger.valueOf(1L));
assertFalse(p1.equals(p2) || p2.equals(p1));
// test case 3: not equal objects - y
p1 = new ECPoint(BigInteger.valueOf(-23457L), BigInteger.ONE);
p2 = new ECPoint(BigInteger.valueOf(-23456L), BigInteger.ZERO);
assertFalse(p1.equals(p2) || p2.equals(p1));
// test case 4: not equal - some point and POINT_INFINITY
p1 = ECPoint.POINT_INFINITY;
p2 = new ECPoint(BigInteger.valueOf(-23456L), BigInteger.ZERO);
assertFalse(p1.equals(p2) || p2.equals(p1));
}
Aggregations