A hashing algorithm is weak if it is easy to determine the original input from the hash or to find another input that yields the same hash. Weak hashing algorithms can lead to security vulnerabilities.
1// Noncompliant: Used `NullCipher`, which will not use any encryption.
2fun noncompliant(plainText: String): Array<Byte> {
3 val doNothingCipher: Cipher = NullCipher()
4 val cipherText: Cipher = doNothingCihper.doFinal(plainText)
5 return cipherText
6}
1// Compliant: Avoided use of `NullCipher` while encrypting value
2fun compliant(plainText: String): Void {
3 val cipher: Cipher = Cipher.getInstance("AES/CBC/PKCS5Padding")
4 val cipherText: Array<Byte> = cipher.doFinal(plainText)
5 return cipherText
6}
1// Noncompliant: Using weak hashing algorithm which is insecure
2fun noncompliant(password: String): ByteArray {
3 val md5Digest: MessageDigest = MessageDigest.getInstance("MD5")
4 md5Digest.update(password.getBytes())
5 val hashValue: ByteArray = md5Digest.digest()
6 return hashValue
7}
1// Compliant: Using secure hashing algorithm
2fun compliant(password: String): ByteArray {
3 val shaDigest: MessageDigest = MessageDigest.getInstance("SHA-256")
4 shaDigest.update(password.getBytes())
5 val hashValue: ByteArray = shaDigest.digest()
6 return hashValue
7}