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