|
| 1 | +# Unsafe usage of weak hash algorithms (CWE-327) |
| 2 | + |
| 3 | +# BAD: Using MD5 - cryptographically broken |
| 4 | +$md5 = [System.Security.Cryptography.MD5]::Create() |
| 5 | +$md5Hash = $md5.ComputeHash([System.Text.Encoding]::UTF8.GetBytes("password123")) |
| 6 | + |
| 7 | +# BAD: Using MD5CryptoServiceProvider |
| 8 | +$md5Provider = New-Object System.Security.Cryptography.MD5CryptoServiceProvider |
| 9 | +$md5ProviderHash = $md5Provider.ComputeHash([System.Text.Encoding]::UTF8.GetBytes("secret")) |
| 10 | + |
| 11 | +# BAD: Using SHA1 - cryptographically weak |
| 12 | +$sha1 = [System.Security.Cryptography.SHA1]::Create() |
| 13 | +$sha1Hash = $sha1.ComputeHash([System.Text.Encoding]::UTF8.GetBytes("password123")) |
| 14 | + |
| 15 | +# BAD: Using SHA1CryptoServiceProvider |
| 16 | +$sha1Provider = New-Object System.Security.Cryptography.SHA1CryptoServiceProvider |
| 17 | +$sha1ProviderHash = $sha1Provider.ComputeHash([System.Text.Encoding]::UTF8.GetBytes("secret")) |
| 18 | + |
| 19 | +# BAD: Using SHA1Managed |
| 20 | +$sha1Managed = New-Object System.Security.Cryptography.SHA1Managed |
| 21 | +$sha1ManagedHash = $sha1Managed.ComputeHash([System.Text.Encoding]::UTF8.GetBytes("data")) |
| 22 | + |
| 23 | +# --------------------------------------------------------- |
| 24 | +# GOOD: Safe usage of cryptographically secure algorithms |
| 25 | +# --------------------------------------------------------- |
| 26 | + |
| 27 | +# GOOD: Using SHA256 |
| 28 | +$sha256 = [System.Security.Cryptography.SHA256]::Create() |
| 29 | +$sha256Hash = $sha256.ComputeHash([System.Text.Encoding]::UTF8.GetBytes("password123")) |
| 30 | + |
| 31 | +# GOOD: Using SHA256CryptoServiceProvider |
| 32 | +$sha256Provider = New-Object System.Security.Cryptography.SHA256CryptoServiceProvider |
| 33 | +$sha256ProviderHash = $sha256Provider.ComputeHash([System.Text.Encoding]::UTF8.GetBytes("secret")) |
| 34 | + |
| 35 | +# GOOD: Using SHA256Managed |
| 36 | +$sha256Managed = New-Object System.Security.Cryptography.SHA256Managed |
| 37 | +$sha256ManagedHash = $sha256Managed.ComputeHash([System.Text.Encoding]::UTF8.GetBytes("data")) |
| 38 | + |
| 39 | +# GOOD: Using SHA384 |
| 40 | +$sha384 = [System.Security.Cryptography.SHA384]::Create() |
| 41 | +$sha384Hash = $sha384.ComputeHash([System.Text.Encoding]::UTF8.GetBytes("password123")) |
| 42 | + |
| 43 | +# GOOD: Using SHA512 |
| 44 | +$sha512 = [System.Security.Cryptography.SHA512]::Create() |
| 45 | +$sha512Hash = $sha512.ComputeHash([System.Text.Encoding]::UTF8.GetBytes("password123")) |
0 commit comments