storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/mint/build/aws-sdk-java/src/Utils.java (about)

     1  /*
     2  *  Mint, (C) 2018 Minio, Inc.
     3  *
     4  *  Licensed under the Apache License, Version 2.0 (the "License");
     5  *  you may not use this file except in compliance with the License.
     6  *  You may obtain a copy of the License at
     7  *
     8  *      http://www.apache.org/licenses/LICENSE-2.0
     9  *
    10  *  Unless required by applicable law or agreed to in writing, software
    11  *
    12  *  distributed under the License is distributed on an "AS IS" BASIS,
    13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  *  See the License for the specific language governing permissions and
    15  *  limitations under the License.
    16  */
    17  
    18  
    19  package io.minio.awssdk.tests;
    20  
    21  import java.io.*;
    22  import java.nio.channels.*;
    23  import java.security.*;
    24  
    25  class Utils {
    26  
    27      public static byte[] createChecksum(InputStream is, int skip, int length) throws Exception {
    28          int numRead;
    29          byte[] buffer = new byte[1024];
    30  
    31          MessageDigest complete = MessageDigest.getInstance("MD5");
    32  
    33          if (skip > -1 && length > -1) {
    34              is = new LimitedInputStream(is, skip, length);
    35          }
    36  
    37          do {
    38              numRead = is.read(buffer);
    39              if (numRead > 0) {
    40                  complete.update(buffer, 0, numRead);
    41              }
    42          } while (numRead != -1);
    43  
    44          return complete.digest();
    45      }
    46  
    47      public static String getInputStreamMD5(InputStream is) throws Exception {
    48          return getInputStreamMD5(is, -1, -1);
    49      }
    50  
    51      public static String getInputStreamMD5(InputStream is, int start, int length) throws Exception {
    52          byte[] b = createChecksum(is, start, length);
    53          String result = "";
    54  
    55          for (int i=0; i < b.length; i++) {
    56              result += Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
    57          }
    58          return result;
    59      }
    60  
    61      public static String getFileMD5(String filePath) throws Exception {
    62          return getFileMD5(filePath, -1, -1);
    63      }
    64  
    65      public static String getFileMD5(String filePath, int start, int length) throws Exception {
    66          File f = new File(filePath);
    67          InputStream is = new FileInputStream(f);
    68          return getInputStreamMD5(is, start, length);
    69      }
    70  
    71      public static String getBufferMD5(byte[] data) throws Exception {
    72          ByteArrayInputStream bis = new ByteArrayInputStream(data);
    73          return getInputStreamMD5(bis);
    74      }
    75  }
    76