Blame SOURCES/TestECDSA.java

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