JavaScript hashing speed comparison: MD5 versus SHA-256

JavaScript hashing speed comparison: MD5 versus SHA-256

Hashing algorithms convert input data into a fixed-size string of characters, known as a hash value or digest. These algorithms are one-way functions, meaning the original data cannot be feasibly retrieved from the hash, which makes them useful for data integrity, password storage, and digital signatures. MD5 and SHA-256 are two such hashing algorithms with significant differences in security, speed, and application. MD5 is older and often said to faster hashing due to its simpler structure. MD5 is now considered cryptographically broken. SHA-256 provides a higher level of security with its 256-bit hash output.

But is MD5 really faster? I decided to write a little JavaScript program to hash a large input using both MD5 and SHA-256. My JavaScript code defines two functions for hashing: md5Hash uses the MD5 algorithm to produce a 128-bit hash, while sha256Hash employs SHA-256 for a more secure. A large 1GB array, created by createRandomUint8Array, is then used to benchmark these hashing functions.

function md5Hash(message) {
    return crypto.createHash('md5').update(message).digest('hex');
}

function sha256Hash(message) {
    return crypto.createHash('sha256').update(message).digest('hex');
}

const largeString = createRandomUint8Array(1000000000); // 1GB string

bench('MD5 Hash', () => {
    md5Hash(largeString);
});

bench('SHA-256 Hash', () => {
    sha256Hash(largeString);
});

        

I use the latest version of the Bun runtime and Node.js 23 in my tests. In find that on a ARM systems (Apple M2 or Amazon Graviton 4), Bun is slightly faster on the MD5 benchmark, but both Node.js and Bun have otherwise identical speeds.



My results suggest that you should probably not be using MD5. MD5 is slower than SHA-256 and not as safe.

Even though SHA-256 looks more expensive on paper, modern processors typically have cryptographic extensions to accelerate it.

My code is available on GitHub.


要查看或添加评论,请登录

Daniel Lemire的更多文章

  • Multiplying by the inverse is not the same as the division

    Multiplying by the inverse is not the same as the division

    In school, we learn that the division is the same as multiplying the inverse (or reciprocal), that is x / y = x (1/y)…

    19 条评论
  • Speeding up C++ code with template lambdas

    Speeding up C++ code with template lambdas

    Let us consider a simple C++ function which divides all values in a range of integers: A division between two integers…

  • An overview of parallel programming (Go edition)

    An overview of parallel programming (Go edition)

    In practice, the software we write runs on several processors. Unfortunately, much of what we take for granted on a…

  • How fast can you open 1000 files?

    How fast can you open 1000 files?

    Jarred Sumner, the main author of the Bun JavaScript engine, commented a few days ago on X that opening many files on…

    1 条评论
  • AVX-512 gotcha: avoid compressing words to memory with AMD Zen 4 processors

    AVX-512 gotcha: avoid compressing words to memory with AMD Zen 4 processors

    Convention computer instructions operate on a single piece of data at once (e.g.

    4 条评论
  • Thread-safe memory copy

    Thread-safe memory copy

    A common operation in software is the copy of a block of memory. In C/C++, we often call the function memcpy for this…

    2 条评论
  • Programmer time and the pitfalls of wasteful work

    Programmer time and the pitfalls of wasteful work

    Programmer time is precious. This realization should shape our approach to software development, focusing our efforts…

  • Regular expressions can blow up!

    Regular expressions can blow up!

    Regular expressions, often abbreviated as regex, are a powerful tool for pattern matching within text. For example, the…

    6 条评论
  • Checking whether an ARM NEON register is zero

    Checking whether an ARM NEON register is zero

    Your phone probably runs on 64-bit ARM processors. These processors are ubiquitous: they power the Nintendo Switch…

  • Counting the digits of 64-bit integers

    Counting the digits of 64-bit integers

    Given an integer in software, you may want to know how many decimal digits it needs. For example, the integer 100…

    3 条评论

社区洞察

其他会员也浏览了