Blame SOURCES/TestECDSA.java

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