Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.springcryptoutils.core.digest;
  
  
Default implementation of the message digester.

Author(s):
Mirko Caserta (mirko.caserta@gmail.com)
 
 public class DigesterImpl implements DigesterInitializingBean {

Enum for the output mode: base64 or hexadecimal.
 
 	public static enum OutputMode {
 		BASE64, HEX
 	}
 
 	private String algorithm = "SHA1";
 
 
 	private String charsetName = "UTF-8";
 
 	private String provider;
 
 	private MessageDigest digester;

Sets the digest algorithm. Default is SHA1.

Parameters:
algorithm the digest algorithm
 
 	public void setAlgorithm(String algorithm) {
 		this. = algorithm;
 	}

Sets the output mode. Default is HEX.

Parameters:
outputMode the output mode
 
 	public void setOutputMode(OutputMode outputMode) {
 		this. = outputMode;
 	}

Sets the provider name of the specific implementation requested (e.g., "BC" for BouncyCastle, "SunJCE" for the default Sun JCE provider).

Parameters:
provider the provider to set
 
 	public void setProvider(String provider) {
 		this. = provider;
 	}

Sets the charset to use when converting the message into a raw byte array.

Parameters:
charsetName the charset name
 
 	public void setCharsetName(String charsetName) {
 		this. = charsetName;
 	}

Returns the message digest. The representation of the message digest depends on the outputMode property.

Parameters:
message the message
Returns:
the message digest
See also:
setOutputMode(com.springcryptoutils.core.digest.DigesterImpl.OutputMode)
 
 	public String digest(String message) {
 		final byte[] messageAsByteArray;
 
 		try {
 			messageAsByteArray = message.getBytes();
 		} catch (UnsupportedEncodingException e) {
 			throw new DigestException("error converting message to byte array: charsetName=" + e);
 		}
 
 		final byte[] digest = digest(messageAsByteArray);
 
 		switch () {
 			case :
 				return Base64.encodeBase64String(digest);
 			case :
 				return Hex.encodeHexString(digest);
 			default:
 				return null;
		}
	}

Returns the message digest in raw bytes format.

Parameters:
message the message
Returns:
the message digest
	public byte[] digest(byte[] message) {
		return .digest(message);
	}
		 = ((( == null) || (.length() == 0)) ? MessageDigest.getInstance() : MessageDigest
	}
New to GrepCode? Check out our FAQ X