• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

stdin redirection, or a java question

CTho9305

Elite Member
I need to md5sum some text from within a java app. For some reason the following doesn't work:
echo "mytext" | md5sum
it just prints mytext instead of the md5 hash. Is there a way to do something like:
md5sum < "mytext"
I know you can redirect from a file, but can you send a string as the input? Or is there a better way to MD5 a string in java?
 
Sorry, but I dont exactly understand what this has to do with java...

edit:
echo test | md5 works fine for me, it replies with d8e8fca2dc0f896fd7cb4cb0031ba249
 
It probably has to do with Java because he's calling md5sum within a Java app using System.Runtime.exec() or something like that.

Anyhow, it's common with CLI utilities that a filename of - (that's the minus symbol) reads input from stdin. So this should work:

echo "hello" | md5sum -

Java performance is almost always "good enough" (sometimes only after profiling/optimization) but you're probably better off calling the system's native md5sum instead of using a pure Java library. I once experimented with a Java MD4 hash library vs. a native library, and the difference was very significant for larger files.

Edit:
Forgot to mention there is an MD5 message digest routine in the standard Java crypto library (java.security I think). Runtime.exec() should be just as easy to use, if not easier.
 
Back
Top