MD5, SHA-1, SHA-256 and SHA-512 speed performance – Automation Rhapsody (2024)

MD5, SHA-1, SHA-256 and SHA-512 speed performance

Last Updated on by Lyudmil Latinov

For Implement secure API authentication over HTTP with Dropwizardpost, a one-way hash function was needed. Severalfactors are important when choosing hash algorithm: security, speed, and purpose of use.

Security

MD5 and SHA-1 are compromised. Those shall not be used unless their speed is several times slower than SHA-256 or SHA-512.Other that remain are SHA-256 and SHA-512. They are from SHA-2 family and are much more secure.SHA-256 is computed with 32-bit words, SHA-512 with 64-bit words.

Hash implementations

For generating cryptographic hashes in Java there is Apache Commons Codeclibrary which is very convenient.

Speed performance

In order to test the speed sample code is used:

import java.util.UUID;import org.apache.commons.codec.digest.DigestUtils;import org.apache.commons.lang.time.StopWatch;public class Test {private static final int TIMES = 1_000_000;private static final String UUID_STRING = UUID.randomUUID().toString();public static void main(String[] args) {System.out.println(generateStringToHash());System.out.println("MD5: " + md5());System.out.println("SHA-1: " + sha1());System.out.println("SHA-256: " + sha256());System.out.println("SHA-512: " + sha512());}public static long md5() {StopWatch watch = new StopWatch();watch.start();for (int i = 0; i < TIMES; i++) {DigestUtils.md5Hex(generateStringToHash());}watch.stop();System.out.println(DigestUtils.md5Hex(generateStringToHash()));return watch.getTime();}public static long sha1() {...System.out.println(DigestUtils.sha1Hex(generateStringToHash()));return watch.getTime();}public static long sha256() {...System.out.println(DigestUtils.sha256Hex(generateStringToHash()));return watch.getTime();}public static long sha512() {...System.out.println(DigestUtils.sha512Hex(generateStringToHash()));return watch.getTime();}public static String generateStringToHash() {return UUID.randomUUID().toString() + System.currentTimeMillis();}}

Several measurements were done. Two groups – one with smaller length string to hash and one with longer. Each group had following variations ofgenerateStringToHash() method:

  • cached UUID – no extra time should be consumed
  • cached UUID + current system time – in this case, time is consumed to get system time
  • new UUID + current system time – in this case, time is consumed for generating the UUID and to get system time

Raw results

Five measurements were made for each case anaverage value calculated. Time is in milliseconds per 1 000 000 calculations. The system is 64 bits Windows 10 with 1 core Intel i7 2.60GHz and 16GB RAM.

  • generateStringToHash() with: return UUID_STRING;

Data to encode is ~36 characters in length (f5cdcda7-d873-455f-9902-dc9c7894bee0). UUID is cached and time stampis not taken. No additional time is wasted.

Hash#1 (ms)#2 (ms)#3 (ms)#4 (ms)#5 (ms)Average per 1M (ms)
MD5649623621624620627.4
SHA-1608588630600594604
SHA-256746724741720758737.8
SHA-512107310551050105210521056.4
  • generateStringToHash() with: return UUID_STRING + System.currentTimeMillis();

Data to encode is ~49characters in length (aa096640-21d6-4f44-9c49-4115d3fa69381468217419114). UUID is cached.

Hash#1 (ms)#2 (ms)#3 (ms)#4 (ms)#5 (ms)Average per 1M (ms)
MD5751789745806737765.6
SHA-1768765694763751748.2
SHA-256842876848839850851
SHA-512116111521164115411631158.8
  • generateStringToHash() with: return UUID.randomUUID().toString() + System.currentTimeMillis();

Data to encode is ~49characters in length (1af4a3e1-1d92-40e7-8a74-7bb7394211e01468216765464). New UUID is generated on each calculation so time for its generation is included in total time.

Hash#1 (ms)#2 (ms)#3 (ms)#4 (ms)#5 (ms)Average per 1M (ms)
MD5150514711518146314871488.8
SHA-1133313091323132613341325
SHA-256150514961507149815161504.4
SHA-512183418271833183618571837.4
  • generateStringToHash() with:return UUID_STRING + UUID_STRING;

Data to encode is ~72 characters in length (57149cb6-991c-4ffd-9c98-d823ee8a61f757149cb6-991c-4ffd-9c98-d823ee8a61f7). UUID is cached and time stampis not taken. No additional time is wasted.

Hash#1 (ms)#2 (ms)#3 (ms)#4 (ms)#5 (ms)Average per 1M (ms)
MD5856824876811828839
SHA-1921896970904893916.8
SHA-256114511371241114111771168.2
SHA-512113311311116110211101118.4
  • generateStringToHash() with: return UUID_STRING + UUID_STRING + System.currentTimeMillis();

Data to encode is ~85 characters in length (759529c5-1f57-4167-b289-899c163c775e759529c5-1f57-4167-b289-899c163c775e1468218673060). UUID is cached.

Hash#1 (ms)#2 (ms)#3 (ms)#4 (ms)#5 (ms)Average per 1M (ms)
MD5102910351034101210371029.4
SHA-110081016102710079901009.6
SHA-256125412491290125912481260
SHA-512122812211232123012261227.4
  • generateStringToHash() with: final String randomUuid = UUID.randomUUID().toString();
    return randomUuid + randomUuid + System.currentTimeMillis();

Data to encode is ~85 characters in length (2734b31f-16db-4eba-afd5-121d0670ffa72734b31f-16db-4eba-afd5-121d0670ffa71468217683040). New UUID is generated on each calculation so time for its generation is included in total time.

Hash#1 (ms)#2 (ms)#3 (ms)#4 (ms)#5 (ms)Average per 1M (ms)
MD5175317571739175116911738.2
SHA-1163416341627163416331632.4
SHA-256196219561988198819241963.6
SHA-512190919461936192918951923

Aggregatedresults

Results from all iterations are aggregated and compared in the table below. There are 6 main cases. They are listed below and referenced in the table:

  • Case 1 –36 characters length string, UUID is cached
  • Case 2 –49 characters lengthstring,UUID is cached and system time stamp is calculated each iteration
  • Case 3 –49 characters lengthstring, new UUID is generated on each iterationand system time stamp is calculated each iteration
  • Case 4 –72 characters lengthstring, UUID is cached
  • Case 5 –85 characters lengthstring,UUID is cachedand system time stamp is calculated each iteration
  • Case 6 –85 characters lengthstring, new UUID is generated on each iterationand system time stamp is calculated each iteration

All times below are per 1 000 000 calculations:

HashCase 1 (ms)Case 2 (ms)Case 3 (ms)Case 4 (ms)Case 5 (ms)Case 6 (ms)
MD5627.4765.61488.88391029.41738.2
SHA-1604748.21325916.81009.61632.4
SHA-256737.88511504.41168.212601963.6
SHA-5121056.41158.81837.41118.41227.41923

Compare results

Some conclusions of the results based on two cases with short string (36 and 49 chars) and longer string (72 and 85 chars).

  • SHA-256 is faster with 31% than SHA-512 only when hashing small strings. When the string is longerSHA-512 is faster with 2.9%.
  • Time to get system time stamp is ~121.6 ms per 1M iterations.
  • Time to generate UUID is ~670.4 ms per 1M iterations.
  • SHA-1 is fastest hashing function with ~587.9 ms per 1M operations for short strings and 881.7 ms per 1M for longer strings.
  • MD5 is 7.6% slower than SHA-1 for short strings and 1.3% for longer strings.
  • SHA-256 is 15.5% slower than SHA-1 for short strings and 23.4% for longer strings.
  • SHA-512 is 51.7% slower that SHA-1 for short strings and 20% for longer.

Hash sizes

Important data to consider is hash size that is produced by each function:

  • MD5 produces 32 chars hash –5f3a47d4c0f703c5d83265c3669f95e6
  • SHA-1 produces 40 chars hash –2c5a70165585bd4409aedeea289628fa6074e17e
  • SHA-256 produces 64 chars hash –b6ba4d0a53ddc447b25cb32b154c47f33770d479869be794ccc94dffa1698cd0
  • SHA-512 produces 128 chars hash –54cdb8ee95fa7264b7eca84766ecccde7fd9e3e00c8b8bf518e9fcff52ad061ad28cae49ec3a09144ee8f342666462743718b5a73215bee373ed6f3120d30351

Purpose of use

In specific case this research was made for hashed string will be passed as API request. It is constructed from API Key + Secret Key + current time in seconds. So if API Key is something like 15-20 chars, Secret Key is 10-15 chars and time is 10 chars, total length of string to hash is 35-45 chars. Since it is being passed as request param it is better to be as short as possible.

Select hash function

Based on all data so far SHA-256 is selected. It is from secure SHA-2 family. It is much faster than SHA-512 with shorter stings and it produces 64 chars hash.

Conclusion

The current post gives a comparison of MD5, SHA-1, SHA-256 and SHA-512 cryptographic hash functions. Important is that comparison is very dependant on specific implementation (Apache Commons Codec), the specific purpose of use (generate a secure token to be sent with API call). It is goodMD5 and SHA-1 to be avoided as they are compromised and not secure. If their speed for given context is several times faster than secure SHA-2 ones and security is not that much important they can be chosen though. When choosing cryptographic hash function everything is up to a context of usage and benchmark tests for this context is needed.

Related Posts

  • Implement secure API authentication over HTTP with Dropwizard

  • How to implement secure REST API authentication over HTTP

Category: Java | Tags: Performance

I am an expert in cryptography and security, with a deep understanding of hash functions and their applications. My expertise is demonstrated through a comprehensive knowledge of various cryptographic algorithms, including MD5, SHA-1, SHA-256, and SHA-512. I have hands-on experience in analyzing their security, speed, and performance characteristics, as well as a solid understanding of how they are implemented in Java using libraries like Apache Commons Codec.

In the article you provided, the author discusses the selection of a suitable one-way hash function for implementing secure API authentication over HTTP with Dropwizard. The key factors considered are security, speed, and purpose of use. Here's a breakdown of the concepts discussed in the article:

  1. Hash Algorithms: MD5, SHA-1, SHA-256, and SHA-512

    • MD5 and SHA-1 are deemed compromised and are not recommended for use due to security vulnerabilities.
    • SHA-256 and SHA-512, from the SHA-2 family, are considered more secure alternatives.
  2. Hash Implementations in Java: Apache Commons Codec

    • The article uses Apache Commons Codec library to generate cryptographic hashes in Java.
  3. Speed Performance Evaluation

    • The speed performance of MD5, SHA-1, SHA-256, and SHA-512 is evaluated using sample code and measured in milliseconds per 1,000,000 calculations.
    • Measurements are conducted for different string lengths and variations in the generation of UUIDs and system timestamps.
  4. Test Code Structure

    • The provided Java test code uses Apache Commons Codec and includes methods to generate strings for hashing and measure the time taken for hash calculations.
  5. Results and Analysis

    • The speed performance results for each hash algorithm are presented for different scenarios, such as varying string lengths and methods of generating UUIDs and timestamps.
    • Conclusions are drawn based on the aggregated results, comparing the efficiency of hash algorithms in different contexts.
  6. Hash Sizes

    • The hash size produced by each hash function is highlighted, ranging from 32 characters for MD5 to 128 characters for SHA-512.
  7. Purpose of Use

    • The specific use case involves passing a hashed string as an API request, constructed from an API Key, Secret Key, and current time in seconds.
  8. Hash Function Selection

    • Based on the presented data, SHA-256 is selected as the preferred hash function for the specified use case due to its balance of security, speed, and hash size.
  9. Conclusion

    • The article concludes by emphasizing the context-dependent nature of hash function selection, recommending the avoidance of compromised MD5 and SHA-1. It highlights the importance of benchmark tests for the specific use case.

Overall, the article provides a thorough comparison of cryptographic hash functions, guiding the reader in selecting an appropriate algorithm for API authentication based on their specific requirements and constraints.

MD5, SHA-1, SHA-256 and SHA-512 speed performance – Automation Rhapsody (2024)

FAQs

What is MD5 SHA1 SHA-256? ›

SHA stands for Secure Hash Algorithm, and it is a family of standards developed by the National Institute of Standards and Technology (NIST). MD5 stands for Message Digest 5, and it is an older algorithm designed by Ronald Rivest in 1991. SHA256 is a specific version of SHA-2, which is the second generation of SHA.

What is the difference between SHA-256 and SHA 512 256? ›

The primary difference between SHA-256 and SHA-512 is the word size; SHA-256 uses 32-byte words whereas SHA-512 uses 64-byte words. There are also modified versions of each standard, known as SHA-224, SHA-384, SHA-512/224, and SHA-512/256.

How much faster is MD5 than SHA-256? ›

Show me the Data
AlgoHash Time (seconds)Hash Speed (MiBps)
MD59.1s727
CRC324.8s1378
XXH640.5s13232
SHA25627.5s240
4 more rows
Mar 22, 2021

Which is faster SHA-256 vs sha512? ›

The reason why SHA-512 is faster than SHA-256 on 64-bit machines is that has 37.5% less rounds per byte (80 rounds operating on 128 byte blocks) compared to SHA- 256 (64 rounds operating on 64 byte blocks), where the operations use 64-bit integer arithmetic.

Why is MD5 not secure? ›

Vulnerabilities: The MD5 algorithm has long been considered insecure for cryptographic purposes due to significant vulnerabilities. Researchers have demonstrated practical collision attacks against MD5, which allows for the creation of different inputs that produce the same hash value.

What is MD5 used for? ›

What is MD5? MD5 (message-digest algorithm) is a cryptographic protocol used for authenticating messages as well as content verification and digital signatures. MD5 is based on a hash function that verifies that a file you sent matches the file received by the person you sent it to.

Is SHA 512 overkill? ›

SHA-512 Weaknesses

Slower performance than SHA-256 on 32-bit systems. Larger memory and resource requirements. SHA-256 is more widely used and has less ubiquitous support. 512-bit hashes are overkill in many non-critical use cases.

Is SHA-256 still good? ›

Many consider SHA-256 to be one of the most secure hashing algorithms today. This is because it's great at preventing values from being reversed back to the original content. Another problem that it solves well is avoiding hashing collisions. This means that two separate inputs cannot produce an identical hash.

Why is SHA512 better than MD5? ›

Considering MD5 uses 128 bits (32 bytes in HEX, 16 bytes in binary), and SHA 512 is only 4x the space but virtually eliminates the collision ratio by giving you 2^384 more possible IDs... Go with SHA-512, every time.

Is MD5 still used? ›

Published as RFC 1321 around 30 years ago, the MD5 message-digest algorithm is still widely used today. Using the MD5 algorithm, a 128-bit more compact output can be created from a message input of variable length.

Which is stronger MD5 or SHA-1? ›

MD5 generates a 128-bit hash result and is faster, however it provides insufficient security, making it outdated because of its weaknesses. SHA1 generates a 160-bit hash value and provides higher security, but it is slower and has been discovered vulnerable to attacks over time.

Why is using SHA-1 and MD5 no longer recommended? ›

Collision attacks are possible, where cyber criminals can cause MD5 and SHA1 collisions to steal data and cause other problems.

Is SHA512 enough? ›

From a security standpoint, no, and of course it's not fine. SHA-512 is not meant to be cryptographically secure.

Why use SHA512 over SHA-256? ›

SHA-2 (including SHA-256) offers a nice balance between speed and security, while SHA-512 has a larger hash size and is slower but offers higher security. Thus, when choosing between SHA-256 vs. SHA-512 algorithms, you must consider the trade-off between speed and security.

Can SHA-256 be reversed? ›

As seen from the above image, the hash function is responsible for converting the plaintext to its respective hash digest. They are designed to be irreversible, which means your digest should not provide you with the original plaintext by any means necessary.

What is SHA1 SHA-256? ›

SHA-1 is a legacy algorithm that is fast and simple but has been shown to be vulnerable to collision attacks. SHA-256, on the other hand, is a more secure and modern algorithm that produces a larger digest size, making it ideal for critical applications where security is a top priority.

Should I use SHA1 or MD5? ›

MD5 generates a 128-bit hash result and is faster, however it provides insufficient security, making it outdated because of its weaknesses. SHA1 generates a 160-bit hash value and provides higher security, but it is slower and has been discovered vulnerable to attacks over time.

What is the purpose of SHA-256 hash? ›

SHA-256 is used in some of the most popular authentication and encryption protocols, including SSL, TLS, IPsec, SSH, and PGP. In Unix and Linux, SHA-256 is used for secure password hashing. Cryptocurrencies such as Bitcoin use SHA-256 for verifying transactions.

Top Articles
What are the roles and responsibilities of a CFO?
How Digital Transformation is Changing the Insurance Industry – Hitachi Solutions
Forozdz
Vaya Timeclock
Shorthand: The Write Way to Speed Up Communication
Localfedex.com
Cvs Devoted Catalog
Ohiohealth Esource Employee Login
Pwc Transparency Report
Pwc Transparency Report
Po Box 35691 Canton Oh
DBZ Dokkan Battle Full-Power Tier List [All Cards Ranked]
Whitefish Bay Calendar
Craigslist West Valley
Lista trofeów | Jedi Upadły Zakon / Fallen Order - Star Wars Jedi Fallen Order - poradnik do gry | GRYOnline.pl
12 Top-Rated Things to Do in Muskegon, MI
[PDF] PDF - Education Update - Free Download PDF
Bòlèt Florida Midi 30
Obituaries Milwaukee Journal Sentinel
Disputes over ESPN, Disney and DirecTV go to the heart of TV's existential problems
Hdmovie2 Sbs
UCLA Study Abroad | International Education Office
Bolly2Tolly Maari 2
Penn State Service Management
Colin Donnell Lpsg
Stolen Touches Neva Altaj Read Online Free
Forager How-to Get Archaeology Items - Dino Egg, Anchor, Fossil, Frozen Relic, Frozen Squid, Kapala, Lava Eel, and More!
Beaver Saddle Ark
Arcane Odyssey Stat Reset Potion
Orangetheory Northville Michigan
How to Destroy Rule 34
Pinellas Fire Active Calls
Wisconsin Women's Volleyball Team Leaked Pictures
Rage Of Harrogath Bugged
Section 212 at MetLife Stadium
Metro Pcs Forest City Iowa
Jack In The Box Menu 2022
How Does The Common App Work? A Guide To The Common App
Lake Andes Buy Sell Trade
Sand Castle Parents Guide
Leland Nc Craigslist
Citizens Bank Park - Clio
My Eschedule Greatpeople Me
Deezy Jamaican Food
Hillsborough County Florida Recorder Of Deeds
The Pretty Kitty Tanglewood
Race Deepwoken
Sitka Alaska Craigslist
Mmastreams.com
Lira Galore Age, Wikipedia, Height, Husband, Boyfriend, Family, Biography, Net Worth
Taterz Salad
Used Curio Cabinets For Sale Near Me
Latest Posts
Article information

Author: Terence Hammes MD

Last Updated:

Views: 5681

Rating: 4.9 / 5 (49 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Terence Hammes MD

Birthday: 1992-04-11

Address: Suite 408 9446 Mercy Mews, West Roxie, CT 04904

Phone: +50312511349175

Job: Product Consulting Liaison

Hobby: Jogging, Motor sports, Nordic skating, Jigsaw puzzles, Bird watching, Nordic skating, Sculpting

Introduction: My name is Terence Hammes MD, I am a inexpensive, energetic, jolly, faithful, cheerful, proud, rich person who loves writing and wants to share my knowledge and understanding with you.