Blame SOURCES/TestECDSA.java

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