- How to get the MD5 hash of a string directly in the terminal?
- 9 Answers 9
- Is it possible to generate a checksum (md5 ) a string in a shell?
- 3 Answers 3
- Creating an MD5 hash of a string in bash
- Share this:
- Leave a Reply Cancel reply
- Social links
- Google Search
- Most Recent Posts
- Subscribe to Blog via Email
- How to calculate a hash for a string (url) in bash for wget caching
- 4 Answers 4
How to get the MD5 hash of a string directly in the terminal?
How do I get the MD5 hash of a string directly from the terminal? For example, I want the string abcdefg hashed. Currently the md5sum command only accepts a filename as input. I want to simply enter the following line and everything be done with.
md5sum abcdefg output: ac54bcf346e578feb46888b3ecd2344f
9 Answers 9
You can also say something like this :
~$ echo -n Welcome | md5sum 83218ac34c1834c26781fe4bde918ee4 -
It basically does the same thing as described by @enzotib, but is perhaps a bit simpler.
You also want to update the example result, as 7803ffca. is the result with the added newline. The correct result for the command above is 83218ac34c1834c26781fe4bde918ee4 —
Please correct me if I’m wrong, but I think this is because the MD5sum was applied to a stream of data, as opposed to reading a file content, which has a name associated to it.
Notice that the -n is mandatory. Without it, your hash will be totally wrong since it includes the newline character.
@alper echo -n Welcome | md5sum | awk ‘
Very simple, it accepts stdin, so
To avoid the trailing newline added by the shell:
printf '%s' "my string" | md5sum
+1 for using printf correctly. If you want to have the sum without the — , put | cut -d ‘ ‘ -f 1 . Example usage: sum=$(printf ‘%s’ ‘some string’ | md5sum | cut -d ‘ ‘ -f 1)
it’s weird but the
I got wrong result with the first command b9bfa87a6a126911f2246c7a615bff27 — instead of 2ba81a47c5512d9e23c435c1f29373cb —
$ echo -n 123456 | md5sum | awk '' e10adc3949ba59abbe56e057f20f883e
you can create a shell script.
For example,the script name is md5.sh:
#!/bin/bash echo -n $1 | md5sum | awk ''
$ md5.sh 123456 e10adc3949ba59abbe56e057f20f883e
If your system is macOS. You need to modify this script:
$ echo -n 123456 | md5 | awk '' e10adc3949ba59abbe56e057f20f883e
I created a function md5() < echo -n $1 | md5sum | awk '
openssl md5 filename openssl sha1 filename
For string pipe the content using echo
echo -n 123456 | openssl md5
Running md5sum with no arguments at all will cause it to read input from the terminal. Type or paste whatever you want, and when you are done, press ctrl-d to end the input.
@James, if it does not follow a newline, yes. If you hit it after hitting enter, it only needs once. When it does not follow a newline, it just forces all of the characters typed on the line so far to be processed immediately instead of waiting for a newline.
My quick poke at the —help for md5sum demonstrates that the command:
will then give a prompt for simple input. Inputting some text and then using Enter and then Ctrl + D to signify end of file then causes md5sum to spit out the MD5 of the raw text you entered (including that Enter , it’s a CR, IIRC).
Less to type and no piping! And avoiding your plaintext password being recorded in shell history! Woo!
If you do not want that trailing CR (which is usually the case if you want to hash a password), don’t hit Enter before Ctrl + D , enter Ctrl + D twice instead.
There are many examples to do this, but some of them are not equivalent because some of them explicitly or implicitly include the newline, and some others do not.
I would like to clearly specify which of the popular methods includes the newline and which are not.
Here are some examples along to calculating the md5 hash WITHOUT trailing newline (CORRECT):
$ echo -n "test" > test.txt $ wc test.txt 0 1 4 test.txt $ md5sum test.txt 098f6bcd4621d373cade4e832627b4f6 test.txt
Note: -n in echo means: «do not output the trailing newline».
$ echo -n "test" | md5sum 098f6bcd4621d373cade4e832627b4f6 -
$ printf "%s" "test" | md5sum 098f6bcd4621d373cade4e832627b4f6 -
Using only md5sum command:
(Let’s write md5sum , press Enter then write string test and then press double combination Ctrl + d )
$ md5sum test098f6bcd4621d373cade4e832627b4f6 -
(Let’s write md5sum — , press Enter then write string test and then press double combination Ctrl + d )
$ md5sum - test098f6bcd4621d373cade4e832627b4f6 -
Here are some examples along to calculating the md5 hash WITH trailing newline (SO NOT CORRECT):
$ echo "test" > test_n.txt $ wc test_n.txt 1 1 5 test_n.txt $ md5sum test_n.txt d8e8fca2dc0f896fd7cb4cb0031ba249 test_n.txt
Using echo WITHOUT -n inline:
echo "test" | md5sum d8e8fca2dc0f896fd7cb4cb0031ba249 -
Using only md5sum command but with Enter key after writing the text:
(Let’s write md5sum , press Enter then write string test and then press agaien Enter and once combination Ctrl + d )
$ md5sum test d8e8fca2dc0f896fd7cb4cb0031ba249 -
Using md5sum — command but with Enter key after writing the text:
(Let’s write md5sum — , press Enter then write string test and then press agaien Enter and once combination Ctrl + d )
$ md5sum - test d8e8fca2dc0f896fd7cb4cb0031ba249 -
Is it possible to generate a checksum (md5 ) a string in a shell?
I would like to have a unique ID for filenames so I can iterate over the IDs and compare the checksums of the files? Is it possible to have a checksum for the name of the file so I can have a unique ID per filename? I would welcome other ideas.
3 Answers 3
serce@unit:~$ echo "Hello, checksum!" | md5sum 9f898618b071286a14d1937f9db13b8f -
serce@unit:~$ md5sum agent.yml 3ed53c48f073bd321339cd6a4c716c17 -
Yes it is possible using md5sum and basename $0 gives the name of current file
Assuming I have the script as below named md5Gen.sh
#!/bin/bash mdf5string=$(basename "$0" | md5sum ) echo -e `basename "$0"` $mdf5string
Running the script would give me
md5Gen.sh 911949bd2ab8467162e27c1b6b5633c0 -
Yes, it is possible to obtain the MD5 of an string:
$ printf '%s' "This-Filename" | md5sum dd829ba5a7ba7bdf7a391f2e0bd7cd1f -
It is important to understand that there is no newline at the end of the printed string. An equivalent in bash would be to use echo -n :
$ echo -n "This-Filename" | md5sum dd829ba5a7ba7bdf7a391f2e0bd7cd1f -
The -n (valid in bash) is important because otherwise your hash would change with the inclusion of a newline that is not part of the text:
$ echo "This-Filename" | md5sum 7ccba9dffa4baf9ca0e56c078aa09a07 -
That also apply to file contents:
$ echo -n "This-Filename" > infile $ md5sum infile dd829ba5a7ba7bdf7a391f2e0bd7cd1f infile $ echo "This-Filename" > infile $ md5sum infile 7ccba9dffa4baf9ca0e56c078aa09a07 infile
Creating an MD5 hash of a string in bash
Make sure that you are not including the new line character (\n) as well in your string, there are cases where it might be included without you explicitly writing it.
For example if you use the output of an echo command in bash, it will automatically add a new line character to the end and will cause your hash to be not the one you would like to have.
For example, the MD5 hash of ‘bytefreaks.net’ (without the quotes) should be 16c00d9cfaef1688d4f2ddfb11b60f46 but if you execute the following you will see you will get a different result.
echo ‘bytefreaks.net’ | md5sum
01c46835dcb79be359e0b464ae6c6156 —
To avoid this error, use the -n option for echo that will direct the command not to output a trailing new line character.
echo -n ‘bytefreaks.net’ | md5sum
16c00d9cfaef1688d4f2ddfb11b60f46 —
Share this:
This post is also available in: Greek
Leave a Reply Cancel reply
This site uses Akismet to reduce spam. Learn how your comment data is processed.
Social links
Google Search
Most Recent Posts
Subscribe to Blog via Email
- About Us
- Blackberry code computer
- CSV to KML Cell Site Map
- Epoch
- Privacy Policy
- Random Password Generator
- Results
- Sitemap
- User Agent
- What is my IP ?
- XML Tree Visualizer
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy
How to calculate a hash for a string (url) in bash for wget caching
I’m building a little tool that will download files using wget, reading the urls from different files. The same url may be present in different files; the url may even be present in one file several times. It would be inefficient to download a page several times (every time its url found in the list(s)). Thus, the simple approach is to save the downloaded file and to instruct wget not to download it again if it is already there. That would be very straightforward; however the urls are very long (many many GET parameters) and therefore cannot be used as such for filenames (wget gives the error ‘Cannot write to. [] file name too long’). So, I need to rename the downloaded files. But for the caching mechanism to work, the renaming scheme needs to implement «one url <=>one name»: if a given url can have multiple names, the caching does not work (ie, if I simply number the files in the order they are found, I won’t let wget identify which urls have already been downloaded). The simplest renaming scheme would be to calculate an md5 hash of the filename (and not of the file itself, which is what md5sum does); that would ensure the filename is unique and that a given url results in always the same name. It’s possible to do this in Perl, etc., but can it be done directly in bash or using a system utility (RedHat)?=>
4 Answers 4
Sounds like you want the md5sum system utility.
URLMD5=`/bin/echo $URL | /usr/bin/md5sum | /bin/cut -f1 -d" "`
If you want to only create the hash on the filename, you can get that quickly with sed:
FILENAME=`echo $URL | /bin/sed -e 's#.*/##'` URLMD5=`/bin/echo $FILENAME | /usr/bin/md5sum | /bin/cut -f1 -d" "`
Note that, depending on your distribution, the path to cut may be /usr/bin/cut .