Blame SOURCES/TestECDSA.java

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