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