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

org.apache.hadoop.fs.cosn.MD5Utils Maven / Gradle / Ivy

Go to download

This module contains code to support integration with Tencent Cloud COS. It also declares the dependencies needed to work with COS.

There is a newer version: 8.2.7
Show newest version
package org.apache.hadoop.fs.cosn;

import org.apache.hadoop.fs.cosn.buffer.CosNByteBuffer;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public final class MD5Utils {
  public static byte[] calculate(CosNByteBuffer buffer)
      throws NoSuchAlgorithmException, IOException {
    if (null == buffer) {
      return null;
    }

    MessageDigest md5 = MessageDigest.getInstance("MD5");
    InputStream inputStream = new DigestInputStream(new BufferInputStream(buffer), md5);
    byte[] chunk = new byte[(int) (4 * Unit.KB)];
    while(inputStream.read(chunk) != -1);
    return md5.digest();
  }

  public static byte[] calculate(String str) throws NoSuchAlgorithmException {
    if (null == str) {
      return null;
    }
    MessageDigest md5 = MessageDigest.getInstance("MD5");
    return md5.digest(str.getBytes(StandardCharsets.UTF_8));
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy