Blame SOURCES/TestECDSA.java

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