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