Blame SOURCES/TestECDSA.java

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