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