Blame SOURCES/TestECDSA.java

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