Hash stretching

Hash stretching slows down an algorithm by repeating the cryptographic hash function many times over. For example, instead of hashing the password once with SHA-256, we run the SHA-256 on the resulting hash again and again:

function simpleHash(password) {  return SHA256(password);}function repeatedHash(password) {  const iterations = 64000;  let x = 0;  let hash = password;  while (x < iterations) {    hash = SHA256(hash);    x++;  }  return hash;}

The benefit of this method is that you can change the number of iterations to change the time required to run the function. For instance, if the computing power has doubled in the past few years, you can simply double the number of iterations to keep the same level of security.

Get Building Enterprise JavaScript Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.