Blame SOURCES/TestECDSA.java

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