﻿# V5902\. OWASP\. A cryptographically weak hash algorithm is used in security\-sensitive code\. Consider using a stronger alternative\.

The analyzer has detected the use of a cryptographically weak hash algorithm in security\-sensitive code\.

Vulnerabilities related to the use of a cryptographically weak hash algorithm can be categorized under the OWASP Top 10 Application Security Risks 2025 as follows:

* [A04:2025 Cryptographic Failures](https://top10.owasp.org/2025/A04_2025-Cryptographic_Failures/)
* [A07:2025 Authentication Failures](https://top10.owasp.org/2025/A07_2025-Authentication_Failures/)

The example:

```cpp
hash := md5.Sum([]byte(req.Password))
hashStr := fmt.Sprintf("%x", hash)
_, err := DB.Exec("INSERT ....",
                  req.Username,
                  hashStr)
```

In this case, the password hash is calculated using the cryptographically weak [MD5](https://en.wikipedia.org/wiki/MD5#Security) algorithm, making the data potentially vulnerable to brute\-force, rainbow table, collision attacks, etc\.

The fixed example:

```cpp
import (
  ....
  secrand "crypto/rand"
  "crypto/hex"
  "golang.org/x/crypto/argon2"
)
....
func hashPassword(password string) (string, error) {
  salt := make([]byte, 16)
  if _, err := secrand.Read(salt); err != nil {
    return "", err
  }
.
  key := argon2.IDKey(
    []byte(password), 
    salt,        // random 128-bit salt
    argonTime,   // number of passes, e.g. 3
    argonMemory, // memory size in KB, e.g. 64 * 1024
    argonThreads, // degree of parallelism, e.g. 4
    argonKeyLen,  // length of the derived key in bytes, e.g. 32
  )

  // PHC String Format
  return fmt.Sprintf(
    "$argon2id$v=%d$m=%d,t=%d,p=%d$%s$%s",
    argon2.Version, argonMemory, argonTime, argonThreads, 
    hex.EncodeToString(salt),
    hex.EncodeToString(key),
  ), nil
}

....
hashStr, err := hashPassword(req.Password)
....
_, err = DB.Exec("INSERT ....",
                 req.Username,
                 hashStr)
```



In the fixed example, the [Argon2id](https://en.wikipedia.org/wiki/Argon2) algorithm from the `golang.org/x/crypto/argon2` package is used as the hash function\. The comments list the recommended values for the algorithm parameters, as specified in [RFC 9106, Section 4](https://www.rfc-editor.org/rfc/rfc9106.html#section-4-6.2)\. It also requires the generation of a 128\-bit seed, for which a cryptographically secure random number generator from the [`crypto/rand`](https://pkg.go.dev/crypto/rand#Read) package is used\.

You can find more security recommendations on the OWASP Cheat Sheet Series pages:

* [Password Storage Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html)\.
* [Cryptographic Storage Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Cryptographic_Storage_Cheat_Sheet.html)\.