Blame SOURCES/TestECDSA.java

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