Importing SVN archives - B6M1
This commit is contained in:
commit
fc4c6bd340
1695 changed files with 98617 additions and 0 deletions
legacyworlds-utils/src/main/java/com/deepclone/lw/utils
|
@ -0,0 +1,56 @@
|
|||
package com.deepclone.lw.utils;
|
||||
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
import org.apache.commons.codec.binary.Hex;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* This class provides a static method which simplies computing hex-encoded digests of strings using
|
||||
* some hashing algorithm.
|
||||
*
|
||||
* @author tseeker
|
||||
*/
|
||||
public final class DigestHelper
|
||||
{
|
||||
|
||||
private DigestHelper( )
|
||||
{
|
||||
// EMPTY
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This method computes a hex-encoded digest of a string using the specified algorithm.
|
||||
*
|
||||
* @param algo
|
||||
* the digest algorithm to use
|
||||
* @param source
|
||||
* the string to digest
|
||||
* @return the hex-encoded digest
|
||||
*/
|
||||
public static String digest( String algo , String source )
|
||||
{
|
||||
// Create digest
|
||||
MessageDigest digestAlgorithm;
|
||||
try {
|
||||
digestAlgorithm = MessageDigest.getInstance( algo );
|
||||
} catch ( NoSuchAlgorithmException e ) {
|
||||
throw new RuntimeException( "'" + algo + "' digest not supported" );
|
||||
}
|
||||
// Add data
|
||||
try {
|
||||
digestAlgorithm.update( source.getBytes( "UTF-8" ) );
|
||||
} catch ( UnsupportedEncodingException e ) {
|
||||
throw new RuntimeException( "UTF-8 not supported? I must be dreaming." );
|
||||
}
|
||||
// Compute digest and return its hexadecimal encoding
|
||||
byte[] digest = digestAlgorithm.digest( );
|
||||
return Hex.encodeHexString( digest );
|
||||
}
|
||||
|
||||
}
|
Reference in a new issue