Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.google.code.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 OutputMode outputMode = .;
 
     private String charsetName = "UTF-8";
 
     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 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.google.code.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);
     }
 
    public void afterPropertiesSet() throws NoSuchAlgorithmException {
         = MessageDigest.getInstance();
    }
New to GrepCode? Check out our FAQ X