Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  
  package rs.baselib.crypto;
  
  import java.io.Reader;
 
The class for signing and verifying signatures.

Author(s):
ralph
 
 public class DataSigner {
 
 	private PrivateKey privateKey;
 	private PublicKey publicKey;

Constructor.
 
 	public DataSigner() throws DecryptionException {
 		this(nullnull);
 	}

Constructor from key pair.

Parameters:
keyPair the key pair to be used
 
 	public DataSigner(KeyPair keyPairthrows DecryptionException {
 		this(keyPair.getPrivate(), keyPair.getPublic());
 	}

Constructor from key pair.

Parameters:
privateKey private key
publicKey public key
 
 	public DataSigner(PrivateKey privateKeyPublicKey publicKeythrows DecryptionException {
 		setPublicKey(publicKey);
 		setPrivateKey(privateKey);
 	}

Constructor from private key.

Parameters:
privateKey the private key to be used
 
 	public DataSigner(PrivateKey privateKeythrows DecryptionException {
         this(privateKeynull);
 	}

Constructor from public key.

Parameters:
publicKey the public key to be used
 
 	public DataSigner(PublicKey publicKeythrows DecryptionException {
         this(nullpublicKey);
 	}

    
Returns the privateKey.

Returns:
the privateKey
 
 	public PrivateKey getPrivateKey() {
 		return ;
 	}

Sets the privateKey.

Parameters:
privateKey the privateKey to set
 
 	public void setPrivateKey(PrivateKey privateKey) {
 		this. = privateKey;
 	}

Sets the privateKey.

Parameters:
privateKey the privateKey to set
 
 	public void setPrivateKey(String privateKeythrows SigningException {
 		try {
 			setPrivateKey(EncryptionUtils.decodeBase64PrivateKey(privateKey));
 		} catch (Throwable t) {
 			throw new SigningException(t);
 		}
 	}

Returns the publicKey.

Returns:
the publicKey
 
	public PublicKey getPublicKey() {
		return ;
	}

Sets the publicKey.

Parameters:
publicKey the publicKey to set
	public void setPublicKey(PublicKey publicKey) {
		this. = publicKey;
	}

Sets the publicKey.

Parameters:
publicKey the publicKey to set
	public void setPublicKey(String publicKeythrows SigningException {
		try {
			setPublicKey(EncryptionUtils.decodeBase64PublicKey(publicKey));
catch (Throwable t) {
			throw new SigningException(t);
		}
	}

Creates a signature for the given stream.

Parameters:
reader stream to be signed
Returns:
signature of the provided stream
	public String sign(Reader readerthrows SigningException {
		return Base64.encodeBase64String(getByteSignature(reader)).trim();
	}

Creates a signature for the given stream.

Parameters:
reader stream to be signed
Returns:
signature of the provided stream
	public byte[] getByteSignature(Reader readerthrows SigningException {
		try {
			Signature dsa = Signature.getInstance("SHA1withDSA");
			/* Initializing the object with a private key */
			update(dsareader);
			return dsa.sign();
catch (SigningException e) {
			throw e;
catch (Throwable t) {
			throw new SigningException(t);
		}
	}

Creates a signature for the given stream.

Parameters:
in stream to be signed
Returns:
signature of the provided stream
	public String sign(InputStream inthrows SigningException {
	}

Creates a signature for the given stream.

Parameters:
in stream to be signed
Returns:
signature of the provided stream
	public byte[] getByteSignature(InputStream inthrows SigningException {
		try {
			Signature dsa = Signature.getInstance("SHA1withDSA");
			/* Initializing the object with a private key */
			update(dsain);
			return dsa.sign();
catch (SigningException e) {
			throw e;
catch (Throwable t) {
			throw new SigningException(t);
		}
	}

Updates the DSA with the bytes from this stream.

Parameters:
signature signature
in input stream
Throws:
SigningException when the stream cannot be signed
	public void update(Signature signatureInputStream inthrows SigningException {
		try {
			// Read and update the stream
			int rc = -1;
			byte[] buf = new byte[1024];
			while ((rc = in.read(buf)) >= 0) {
				if (rc != 0) signature.update(buf, 0, rc);
			}
catch (Throwable t) {
			throw new SigningException(t);
		}
	}

Updates the DSA with the bytes from this stream.

Parameters:
signature signature
reader reader for the input stream
Throws:
SigningException when the signing process fails
	protected void update(Signature signatureReader readerthrows SigningException {
		try {
			// Read and update the stream
			int rc = -1;
			char buf[] = new char[1024];
			while ((rc = reader.read(buf)) >= 0) {
				if (rc != 0) {
					String s = new String(buf, 0, rc);
					signature.update(s.getBytes("UTF8"));
				}
			}
catch (Throwable t) {
			throw new SigningException(t);
		}
	}

Creates a signature for the given string.

Parameters:
str string to be signed
Returns:
signature of the provided String
Throws:
SigningException when the stream cannot be signed
	public String sign(String strthrows SigningException {
		return sign(new StringReader(str));
    }

Creates a signature for the given string.

Parameters:
str string to be signed
Returns:
signature of the provided String
Throws:
SigningException when the stream cannot be signed
	public byte[] getByteSignature(String strthrows SigningException {
		return getByteSignature(new StringReader(str));
    }

Creates a signature for the given bytes.

Parameters:
bytes bytes to be signed
Returns:
signature of the provided bytes
Throws:
SigningException when the bytes cannot be signed
	public String sign(byte bytes[]) throws SigningException {
		return sign(new ByteArrayInputStream(bytes));
    }

Creates a signature for the given bytes.

Parameters:
bytes bytes to be signed
Returns:
signature of the provided bytes
Throws:
SigningException when the bytes cannot be signed
	public byte[] getByteSignature(byte bytes[]) throws SigningException {
    }

Verifies the signature on the given stream.

Parameters:
signature signature
in stream to be verified
Returns:
true if the signature confirms stream integrity
Throws:
SigningException when the signature cannot be verified
	public boolean verify(String signatureInputStream inthrows SigningException {
		return verify(EncryptionUtils.decodeBase64(signature), in);
    }

Verifies the signature on the given stream.

Parameters:
signature signature
in stream to be verified
Returns:
true if the signature confirms stream integrity
Throws:
SigningException when the signature cannot be verified
	public boolean verify(byte signature[], InputStream inthrows SigningException {
        try {
            Signature dsa = Signature.getInstance("SHA1withDSA");
            /* Initializing the object with the public key */
            dsa.initVerify(getPublicKey());
            
            /* Update and verify the data */
			update(dsain);
            return dsa.verify(signature);
catch (SigningException e) {
			throw e;
        } catch (Throwable t) {
        	throw new SigningException("Cannot verify signature: "+t.getMessage(), t);
        }
        
    }

Verifies the signature on the given stream.

Parameters:
signature signature
in stream to be verified
Returns:
true if the signature confirms stream integrity
Throws:
SigningException when the signature cannot be verified
	public boolean verify(String signatureReader inthrows SigningException {
        return verify(EncryptionUtils.decodeBase64(signature), in);
    }
    
Verifies the signature on the given stream.

Parameters:
signature signature
in stream to be verified
Returns:
true if the signature confirms stream integrity
Throws:
SigningException when the signature cannot be verified
	public boolean verify(byte signature[], Reader inthrows SigningException {
        try {
            Signature dsa = Signature.getInstance("SHA1withDSA");
            /* Initializing the object with the public key */
            dsa.initVerify(getPublicKey());
            /* Update and verify the data */
			update(dsain);
            return dsa.verify(signature);
catch (SigningException e) {
			throw e;
        } catch (Throwable t) {
        	throw new SigningException("Cannot verify signature: "+t.getMessage(), t);
        }
        
    }
    
Verifies the signature on the given string.

Parameters:
signature signature
str string to be verified
Returns:
true if the signature confirms string integrity
Throws:
SigningException when the signature cannot be verified
    public boolean verify(String signatureString strthrows SigningException {
    	return verify(signaturenew StringReader(str));
    }
    
Verifies the signature on the given bytes.

Parameters:
signature signature
data bytes to be verified
Returns:
true if the signature confirms data integrity
Throws:
SigningException when the signature cannot be verified
    public boolean verify(byte signature[], byte data[]) throws SigningException {
    	return verify(signaturenew ByteArrayInputStream(data));
    }
    
    
New to GrepCode? Check out our FAQ X