|
|
660215 |
/* TestECDSA -- Ensure ECDSA signatures are working.
|
|
|
660215 |
Copyright (C) 2016 Red Hat, Inc.
|
|
|
660215 |
|
|
|
660215 |
This program is free software: you can redistribute it and/or modify
|
|
|
660215 |
it under the terms of the GNU Affero General Public License as
|
|
|
660215 |
published by the Free Software Foundation, either version 3 of the
|
|
|
660215 |
License, or (at your option) any later version.
|
|
|
660215 |
|
|
|
660215 |
This program is distributed in the hope that it will be useful,
|
|
|
660215 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
660215 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
660215 |
GNU Affero General Public License for more details.
|
|
|
660215 |
|
|
|
660215 |
You should have received a copy of the GNU Affero General Public License
|
|
|
660215 |
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
660215 |
*/
|
|
|
660215 |
|
|
|
660215 |
import java.math.BigInteger;
|
|
|
660215 |
import java.security.KeyPair;
|
|
|
660215 |
import java.security.KeyPairGenerator;
|
|
|
660215 |
import java.security.Signature;
|
|
|
660215 |
|
|
|
660215 |
/**
|
|
|
660215 |
* @test
|
|
|
660215 |
*/
|
|
|
660215 |
public class TestECDSA {
|
|
|
660215 |
|
|
|
660215 |
public static void main(String[] args) throws Exception {
|
|
|
660215 |
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");
|
|
|
660215 |
KeyPair key = keyGen.generateKeyPair();
|
|
|
660215 |
|
|
|
660215 |
byte[] data = "This is a string to sign".getBytes("UTF-8");
|
|
|
660215 |
|
|
|
660215 |
Signature dsa = Signature.getInstance("NONEwithECDSA");
|
|
|
660215 |
dsa.initSign(key.getPrivate());
|
|
|
660215 |
dsa.update(data);
|
|
|
660215 |
byte[] sig = dsa.sign();
|
|
|
660215 |
System.out.println("Signature: " + new BigInteger(1, sig).toString(16));
|
|
|
660215 |
|
|
|
660215 |
Signature dsaCheck = Signature.getInstance("NONEwithECDSA");
|
|
|
660215 |
dsaCheck.initVerify(key.getPublic());
|
|
|
660215 |
dsaCheck.update(data);
|
|
|
660215 |
boolean success = dsaCheck.verify(sig);
|
|
|
660215 |
if (!success) {
|
|
|
660215 |
throw new RuntimeException("Test failed. Signature verification error");
|
|
|
660215 |
}
|
|
|
660215 |
System.out.println("Test passed.");
|
|
|
660215 |
}
|
|
|
660215 |
}
|