Blame SOURCES/TestECDSA.java

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