Blame SOURCES/TestECDSA.java

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