001package com.box.sdkgen.internal.utils;
002
003import com.box.sdkgen.box.errors.BoxSDKError;
004import java.security.MessageDigest;
005import java.security.NoSuchAlgorithmException;
006import java.util.Base64;
007
008public class Hash {
009
010  private final HashName algorithm;
011  private final MessageDigest digest;
012
013  public Hash(HashName algorithm) {
014    this.algorithm = algorithm;
015    try {
016      this.digest = MessageDigest.getInstance(algorithm.getValue());
017    } catch (NoSuchAlgorithmException ae) {
018      throw new BoxSDKError("Digest algorithm not found", ae);
019    }
020  }
021
022  public void updateHash(byte[] data) {
023    digest.update(data);
024  }
025
026  public String digestHash(String encoding) {
027    byte[] digestBytes = digest.digest();
028    return Base64.getEncoder().encodeToString(digestBytes);
029  }
030
031  public HashName getAlgorithm() {
032    return algorithm;
033  }
034}