All Downloads are FREE. Search and download functionalities are using the official Maven repository.

xapi.jre.util.DigesterJre Maven / Gradle / Ivy

Go to download

Everything needed to run a comprehensive dev environment. Just type X_ and pick a service from autocomplete; new dev modules will be added as they are built. The only dev service not included in the uber jar is xapi-dev-maven, as it includes all runtime dependencies of maven, adding ~4 seconds to build time, and 6 megabytes to the final output jar size (without xapi-dev-maven, it's ~1MB).

The newest version!
/**
 *
 */
package xapi.jre.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import xapi.annotation.inject.InstanceDefault;
import xapi.util.X_Debug;
import xapi.util.api.Digester;

/**
 * The default {@link Digester} implementation; uses java core MD5 hashing.
 *
 * @author James X. Nelson ([email protected], @james)
 *
 */
@InstanceDefault(implFor = Digester.class)
public class DigesterJre implements Digester {

  protected final MessageDigest digest;

  public DigesterJre() {
    try {
      digest = MessageDigest.getInstance(getAlgorithm());
    } catch (final NoSuchAlgorithmException e) {
      throw X_Debug.rethrow(e);
    }

  }

  public DigesterJre(final MessageDigest instance) {
    digest = instance;
  }

  private String getAlgorithm() {
    return "MD5";
  }

  /**
   * @see xapi.util.api.Digester#update(byte[], int, int)
   */
  @Override
  public Digester update(final byte[] bytes, final int offset, final int length) {
    digest.update(bytes, offset, length);
    return this;
  }

  /**
   * @see xapi.util.api.Digester#digest()
   */
  @Override
  public byte[] digest() {
    return digest.digest();
  }

  /**
   * @see xapi.util.api.Digester#digest(byte[])
   */
  @Override
  public byte[] digest(final byte[] bytes) {
    return digest.digest(bytes);
  }

  /**
   * @see xapi.util.api.Digester#toHexString(byte[])
   */
  @Override
  public String toString(final byte[] bytes) {
    final StringBuilder uuidBuilder = new StringBuilder();
    for (int i = 0; i < bytes.length; i++) {
      uuidBuilder.append(Integer.toString((bytes[i] & 0xff)
                + 0x100, 36).substring(1).toLowerCase());
    }
    return uuidBuilder.toString();
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy