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