Blame SOURCES/TestECDSA.java

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