Blame SOURCES/TestECDSA.java

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