Blame SOURCES/TestECDSA.java

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