---
title: "How can I generate an MD5 hash?"  
description: "How can I generate an MD5 hash?"  
author: "Anonymous User"  
published: 2015-07-03  
updated: 2015-07-03  
canonical: https://www.mindstick.com/forum/23315/how-can-i-generate-an-md5-hash  
category: "java"  
tags: ["java"]  
reading_time: 1 minute  

---

# How can I generate an MD5 hash?

Is there any [method](https://www.mindstick.com/forum/166/webservice-method) to generate [MD5](https://www.mindstick.com/blog/120/implementing-cryptography-by-using-md5-algorithm) [hash](https://www.mindstick.com/forum/159584/what-are-the-differences-between-a-hashmap-and-a-hash-table-in-c-sharp) of a [string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) in [Java](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming)?

## Replies

### Reply by Anonymous User

There's an input stream decorator, java.security.DigestInputStream, so that you can compute the digest while using the input stream as you normally would, instead of having to make an extra pass over the data.\

```
MessageDigest md = MessageDigest.getInstance("MD5");try (InputStream is = Files.newInputStream(Paths.get("file.txt"))) {  DigestInputStream dis = new DigestInputStream(is, md);  /* Read stream to EOF as normal... */}byte[] digest = md.digest();
```

\
Use DigestUtils from Apache Commons Codec library:\

```
FileInputStream fis = new FileInputStream(new File("foo"));String md5 = org.apache.commons.codec.digest.DigestUtils.md5Hex(fis);fis.close() 
```


---

Original Source: https://www.mindstick.com/forum/23315/how-can-i-generate-an-md5-hash

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
