Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package com.cedarsoft.utils.crypt;
 
 import  org.jetbrains.annotations.NonNls;
 import  org.jetbrains.annotations.NotNull;
 
Represents an algorithm
public enum Algorithm {
  MD5( "MD5" ),
  SHA1( "SHA-1""SHA1" ),
  SHA256( "SHA-256""SHA256" ),
  SHA512( "SHA-512""SHA512" ),;
  @NotNull
  public static Algorithm getAlgorithm( @NotNull @NonNls String algorithmString ) {
    //First search for the exact match
    try {
      return Algorithm.valueOfalgorithmString );
    } catch ( IllegalArgumentException ignore ) {
    }
    //Now search for the alternative names
    for ( Algorithm algorithm : Algorithm.values() ) {
      if ( algorithm.alternativeNames.containsalgorithmString ) ) {
        return algorithm;
      }
    }
    throw new IllegalArgumentException"No Alogirthm found for " + algorithmString );
  }
  @NotNull
  @NonNls
  private final List<StringalternativeNames = new ArrayList<String>();

  
Creates a new algorithm. The first alternative name will be used to get the MessageDigest

Parameters:
alternativeNames the alternative names
  Algorithm( @NotNull @NonNls String... alternativeNames ) {
    if ( alternativeNames.length == 0 ) {
      throw new IllegalArgumentException"Need at least one algorithm name" );
    }
    this..addAll( Arrays.asListalternativeNames ) );
  }

  
Returns a list containing all alternative names for the given algorithm

Returns:
the alternative names for the algorithm
  @NotNull
  @NonNls
  public List<? extends StringgetAlternativeNames() {
    return Collections.unmodifiableList );
  }

  
Returns the Message Digest algorithm

Returns:
the message digest
  @NotNull
    try {
      return MessageDigest.getInstance.get( 0 ) );
    } catch ( NoSuchAlgorithmException e ) {
      throw new RuntimeExceptione );
    }
  }
New to GrepCode? Check out our FAQ X