Blame SOURCES/TestECDSA.java

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