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