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