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