Blame SOURCES/TestECDSA.java

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