Skip to main content

Insecure encryption algorithm - SHA1


Upgrade to a secure encryption algorithm


  • Usage of Scala for building scalable and high-performance applications
  • Usage of for cryptographic hashing and message digest operations


Non compliant code


object SHA1Hasher {
def hash(text: String): String = {
val md = MessageDigest.getInstance("SHA-1")
val digest = md.digest()"%02x".format(_)).mkString

The above code shows a Scala function that hashes a given text using the SHA-1 encryption algorithm.

Here's a breakdown of what the code does:

  1. The hash function is defined to take a text string as input.
  2. The getInstance method of MessageDigest is called with "SHA-1" as an argument, which returns an instance of MessageDigest that implements the SHA-1 hash function.
  3. The update method of the MessageDigest instance is called with the bytes of the text string. This method updates the digest using the specified array of bytes.
  4. The digest method of the MessageDigest instance is called, which completes the hash computation. This is where the actual SHA-1 hashing happens.
  5. The resulting digest byte array is then converted to a string of hexadecimal values using the map function.

This code is vulnerable because it uses the SHA-1 algorithm, which is considered insecure for many cryptographic functions. The SHA-1 algorithm has known weaknesses, including vulnerability to collision attacks, where two different inputs produce the same hash output. This could potentially allow an attacker to substitute a malicious file for a legitimate one without detection.


  • Replace the insecure SHA-1 algorithm with a more secure hashing algorithm.
  • Use a cryptographic hash function like SHA-256 or bcrypt.
  • Update the code to use the new hashing algorithm.
  • Ensure that the new algorithm is supported by the server and the environment where the application is deployed.
  • Consider using a salt or pepper to further enhance the security of the hashed values.

Compliant code

import java.nio.charset.StandardCharsets

object SHA256Hasher {
def hash(text: String): String = {
val digest = MessageDigest.getInstance("SHA-256")
val hashedBytes = digest.digest()"%02x".format(_)).mkString

The original code was using the SHA-1 hashing algorithm, which is considered insecure due to its vulnerability to collision attacks. In a collision attack, an attacker can create a different input that produces the same hash as a target input, effectively allowing them to bypass security checks.

The updated code uses the SHA-256 hashing algorithm, which is currently considered secure against collision attacks. The MessageDigest.getInstance("SHA-256") line is where the hashing algorithm is specified. The digest.update(text.getBytes(StandardCharsets.UTF_8)) line is where the input text is converted to bytes and fed into the hashing algorithm. The digest.digest() line is where the hash is actually computed.

The resulting hash is a byte array, so the"%02x".format(_)).mkString line is used to convert this byte array into a string of hexadecimal characters, which is a common way of representing hashes.

Please note that while SHA-256 is currently considered secure, no hashing algorithm can be considered secure forever. As computational power increases and new attacks are discovered, it may become necessary to switch to an even more secure hashing algorithm in the future.

Also, while this code is a good start, it does not include any sort of salt or pepper. A salt is a random value that is generated for each input and combined with the input before hashing. This prevents an attacker from using precomputed tables of hashes (known as rainbow tables) to quickly guess the input that produced a given hash. A pepper is similar, but it is a secret value that is used for all inputs. This provides an additional layer of security even if the attacker is able to obtain the hashes and the salts.