Blame SPECS/TestECDSA.java

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