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

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

Go to download

This module exists solely to package all other gwt modules into a single uber jar. This makes deploying to non-mavenized targets much easier. Of course, you would be wise to inherit your dependencies individually; the uber jar is intended for projects like collide, which have complex configuration, and adding many jars would be a pain.

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