Blame SOURCES/TestECDSA.java

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