Blame SOURCES/TestECDSA.java

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