Blame SOURCES/TestECDSA.java

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