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