2626import com .google .common .io .BaseEncoding ;
2727
2828import io .minio .errors .InsufficientDataException ;
29+ import io .minio .errors .InternalException ;
2930
3031
3132/**
@@ -42,84 +43,85 @@ private Digest() {}
4243 * Returns SHA-256 hash of given string.
4344 */
4445 public static String sha256Hash (String string ) throws NoSuchAlgorithmException {
45- return sha256Hash (string .getBytes (StandardCharsets .UTF_8 ));
46- }
47-
48-
49- /**
50- * Returns SHA-256 hash of given byte array.
51- */
52- public static String sha256Hash (byte [] data ) throws NoSuchAlgorithmException {
53- return sha256Hash (data , data .length );
54- }
55-
56-
57- /**
58- * Returns SHA-256 hash string of given byte array and it's length.
59- */
60- public static String sha256Hash (byte [] data , int length ) throws NoSuchAlgorithmException {
61- MessageDigest messageDigest = MessageDigest .getInstance ("SHA-256" );
62-
63- messageDigest .update (data , 0 , length );
64-
65- return BaseEncoding .base16 ().encode (messageDigest .digest ()).toLowerCase ();
46+ byte [] data = string .getBytes (StandardCharsets .UTF_8 );
47+ MessageDigest sha256Digest = MessageDigest .getInstance ("SHA-256" );
48+ sha256Digest .update ((byte []) data , 0 , data .length );
49+ return BaseEncoding .base16 ().encode (sha256Digest .digest ()).toLowerCase ();
6650 }
6751
6852
6953 /**
70- * Returns SHA-256 of given input stream and it's length.
54+ * Returns SHA-256 hash of given data and it's length.
7155 *
72- * @param inputStream Input stream whose type is either {@link RandomAccessFile} or {@link BufferedInputStream}.
73- * @param len Length of Input stream .
56+ * @param data must be {@link RandomAccessFile}, {@link BufferedInputStream} or byte array .
57+ * @param len length of data to be read for hash calculation .
7458 */
75- public static String sha256Hash (Object inputStream , int len )
76- throws NoSuchAlgorithmException , IOException , InsufficientDataException {
59+ public static String sha256Hash (Object data , int len )
60+ throws NoSuchAlgorithmException , IOException , InsufficientDataException , InternalException {
7761 MessageDigest sha256Digest = MessageDigest .getInstance ("SHA-256" );
78- updateDigests (inputStream , len , sha256Digest , null );
79- return BaseEncoding .base16 ().encode (sha256Digest .digest ()).toLowerCase ();
80- }
81-
82- /**
83- * Returns MD5 hash of given string.
84- */
85- public static String md5Hash (String string ) throws NoSuchAlgorithmException {
86- return md5Hash (string .getBytes (StandardCharsets .UTF_8 ));
87- }
8862
63+ if (data instanceof BufferedInputStream || data instanceof RandomAccessFile ) {
64+ updateDigests (data , len , sha256Digest , null );
65+ } else if (data instanceof byte []) {
66+ sha256Digest .update ((byte []) data , 0 , len );
67+ } else {
68+ throw new InternalException ("Unknown data source to calculate sha256 hash. This should not happen, "
69+ + "please report this issue at https://github.com/minio/minio-java/issues" );
70+ }
8971
90- /**
91- * Returns MD5 hash of given byte array.
92- */
93- public static String md5Hash (byte [] data ) throws NoSuchAlgorithmException {
94- return md5Hash (data , data .length );
72+ return BaseEncoding .base16 ().encode (sha256Digest .digest ()).toLowerCase ();
9573 }
9674
9775
9876 /**
99- * Returns MD5 hash of given byte array and it's length.
77+ * Returns SHA-256 and MD5 hashes of given data and it's length.
78+ *
79+ * @param data must be {@link RandomAccessFile}, {@link BufferedInputStream} or byte array.
80+ * @param len length of data to be read for hash calculation.
10081 */
101- public static String md5Hash (byte [] data , int length ) throws NoSuchAlgorithmException {
102- MessageDigest messageDigest = MessageDigest .getInstance ("MD5" );
82+ public static String [] sha256Md5Hashes (Object data , int len )
83+ throws NoSuchAlgorithmException , IOException , InsufficientDataException , InternalException {
84+ MessageDigest sha256Digest = MessageDigest .getInstance ("SHA-256" );
85+ MessageDigest md5Digest = MessageDigest .getInstance ("MD5" );
10386
104- messageDigest .update (data , 0 , length );
87+ if (data instanceof BufferedInputStream || data instanceof RandomAccessFile ) {
88+ updateDigests (data , len , sha256Digest , md5Digest );
89+ } else if (data instanceof byte []) {
90+ sha256Digest .update ((byte []) data , 0 , len );
91+ md5Digest .update ((byte []) data , 0 , len );
92+ } else {
93+ throw new InternalException ("Unknown data source to calculate sha256 hash. This should not happen, "
94+ + "please report this issue at https://github.com/minio/minio-java/issues" );
95+ }
10596
106- return BaseEncoding .base64 ().encode (messageDigest .digest ());
97+ return new String []{BaseEncoding .base16 ().encode (sha256Digest .digest ()).toLowerCase (),
98+ BaseEncoding .base64 ().encode (md5Digest .digest ())};
10799 }
108100
109101
110102 /**
111- * Returns MD5 hash of given input stream and it's length.
103+ * Returns MD5 hash of given data and it's length.
112104 *
113- * @param inputStream Input stream whose type is either {@link RandomAccessFile} or {@link BufferedInputStream}.
114- * @param len Length of Input stream .
105+ * @param data must be {@link RandomAccessFile}, {@link BufferedInputStream} or byte array .
106+ * @param len length of data to be read for hash calculation .
115107 */
116- public static String md5Hash (Object inputStream , int len )
117- throws NoSuchAlgorithmException , IOException , InsufficientDataException {
108+ public static String md5Hash (Object data , int len )
109+ throws NoSuchAlgorithmException , IOException , InsufficientDataException , InternalException {
118110 MessageDigest md5Digest = MessageDigest .getInstance ("MD5" );
119- updateDigests (inputStream , len , null , md5Digest );
111+
112+ if (data instanceof BufferedInputStream || data instanceof RandomAccessFile ) {
113+ updateDigests (data , len , null , md5Digest );
114+ } else if (data instanceof byte []) {
115+ md5Digest .update ((byte []) data , 0 , len );
116+ } else {
117+ throw new InternalException ("Unknown data source to calculate sha256 hash. This should not happen, "
118+ + "please report this issue at https://github.com/minio/minio-java/issues" );
119+ }
120+
120121 return BaseEncoding .base64 ().encode (md5Digest .digest ());
121122 }
122123
124+
123125 /**
124126 * Updated MessageDigest with bytes read from file and stream.
125127 */
0 commit comments