Skip to content

weak_hash_for_secret

Reports MD5 or SHA-1 protecting a password, a token or a signature. Use a slow salted hash for a password, an HMAC for a signature, or SHA-256 for a fingerprint.

Tier
advisory
Scope
Dart
Lowest preset
recommended
Fix
none
Security class
CWE-328, CWE-916 · mobile risk M10 (insufficient cryptography) · severity 5.3

A convert, startChunkedConversion or bind of package:crypto’s md5 or sha1 that protects a credential. The violation covers the call, and the message names the hash and what it protects. A hash protects a credential when:

  • its input comes from a credential: a text or bytes read by a credential name (password, user.pin, apiKey, a Uint8List secret). The input may be the value directly, or made from it by utf8.encode and the SDK’s other calls that turn text into bytes, its codeUnits, an interpolation ('$salt$password') or a +.
  • its digest goes to a credential or a signature, within the function: a value bound to a name such as passwordHash, hashedPassword, tokenDigest or signature (a variable, an assignment, a named argument or a map entry), or the return value of a function so named (hashPassword).

It does not report:

  • a hash of anything else: a checksum, a cache key, an ETag, a file name, the hash of an e-mail address for an avatar;
  • an Hmac of MD5 or SHA-1, which is still a sound authentication code;
  • SHA-256 and later hashes;
  • a credential passed to your own code first;
  • debug-only code, such as code under kDebugMode, and tests.

MD5 and SHA-1 collisions can be made on ordinary computers, so neither proves that a text is the one signed. Both are also fast: a leaked MD5 of a password is reversed in moments, by tables and graphics cards.

A password needs a slow, salted hash (Argon2, bcrypt, PBKDF2). A signature needs an HMAC or a real signature. A fingerprint needs SHA-256.

Violating
String stored(String password) => md5.convert(utf8.encode(password)).toString();
Correctedhash a password on the server, with a slow hash. Where the app needs a fingerprint of a secret, use SHA-256 or an HMAC
Digest signed(List<int> key, String token) => Hmac(sha256, key).convert(utf8.encode(token));

Set under the assay: key of the project’s root analysis_options.yaml.

Rule parameter Default Effect
enabled true false switches the rule off for the whole project.
exclude [] Path globs where the rule is silent.

In SARIF, the rule’s descriptor carries the tags security, external/cwe/cwe-328, external/cwe/cwe-916 and external/owasp-mobile-2024/m10, and the security-severity 5.3. No other report changes. See Security.

Write why on the line directly above the suppression:

// The legacy server compares this digest; it is replaced in release 4.
// ignore: assay/weak_hash_for_secret

// ignore_for_file: assay/weak_hash_for_secret silences it in a whole file, and // ignore: weak_hash_for_secret without the prefix works too. See Suppressions.