Signature

To implement an ElGamal signature class, we’ll have to implement the Service Provider Interface (SPI) of Signature, which is contained in SignatureSpi . I talked briefly about the SPI in Chapter 3; SignatureSpi contains all the methods you need to define to implement a signature algorithm:

            package oreilly.jonathan.crypto;

import java.io.ByteArrayOutputStream;
import java.math.BigInteger;
import java.security.*;

public class ElGamalSignature
    extends SignatureSpi {

A Signature is intialized with a key, which is used later to either generate or verify a signature value. We’ll save the intialization key in a member variable until we need it:

protected ElGamalKey mKey;

As data is added to our ElGamalSignature with the update() method, we’ll accumulate it in a ByteArrayOutputStream. When the time comes to generate or verify the signature value, we’ll use the data from this stream:

protected ByteArrayOutputStream mOut;

In the signature calculations, later, we’ll frequently make use of the number 1 as a BigInteger. Here, I use a static member variable to hold this special value:

protected static BigInteger kOne = BigInteger.valueOf(1);

When the initVerify() method of Signature is called, it eventually calls the SPI method engineInitVerify() . In our implementation, we first check to make sure we’ve received an ElGamalPublicKey because a public key is always used to verify a signature. Then we save the key and create a new ByteArrayOutputStream to hold the data that will be used to ...

Get Java Cryptography now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.