Skip to content

Commit 446141a

Browse files
committed
Ruby: qhelp for rb/weak-cryptographic-algorithm
1 parent 4234cfe commit 446141a

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
<overview>
6+
<p>
7+
Using broken or weak cryptographic algorithms can leave data
8+
vulnerable to being decrypted or forged by an attacker.
9+
</p>
10+
<p>
11+
Many cryptographic algorithms provided by cryptography
12+
libraries are known to be weak, or flawed. Using such an
13+
algorithm means that encrypted or hashed data is less
14+
secure than it appears to be.
15+
</p>
16+
</overview>
17+
<recommendation>
18+
<p>
19+
Ensure that you use a strong, modern cryptographic
20+
algorithm, such as AES-128 or RSA-2048.
21+
</p>
22+
</recommendation>
23+
<example>
24+
25+
<p>
26+
The following code uses the <code>OpenSSL</code> library to encrypt some
27+
secret data. When you create a cipher using <code>OpenSSL</code> you must
28+
specify the encryption algorithm to use. The first example uses DES, which
29+
is an older algorithm that is now considered weak. The second example uses
30+
AES, which is a stronger modern algorithm.
31+
</p>
32+
33+
<sample src="examples/broken_crypto.rb" />
34+
</example>
35+
<references>
36+
<li>NIST, FIPS 140 Annex a: <a href="http://csrc.nist.gov/publications/fips/fips140-2/fips1402annexa.pdf"> Approved Security Functions</a>.</li>
37+
<li>NIST, SP 800-131A: <a href="http://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar1.pdf"> Transitions: Recommendation for Transitioning the Use of Cryptographic Algorithms and Key Lengths</a>.</li>
38+
<li>OWASP: <a href="https://cheatsheetseries.owasp.org/cheatsheets/Cryptographic_Storage_Cheat_Sheet.html#rule---use-strong-approved-authenticated-encryption">Rule - Use strong approved cryptographic algorithms</a>.
39+
</li>
40+
</references>
41+
</qhelp>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require 'openssl'
2+
3+
class Encryptor
4+
attr_accessor :secret_key
5+
6+
def encrypt_message_weak(message)
7+
cipher = OpenSSL::Cipher.new('des') # BAD: weak encryption
8+
cipher.encrypt
9+
cipher.key = secret_key
10+
cipher.update(message)
11+
cipher.final
12+
end
13+
14+
def encrypt_message_strong(message)
15+
cipher = OpenSSL::Cipher::AES128.new # GOOD: strong encryption
16+
cipher.encrypt
17+
cipher.key = secret_key
18+
cipher.update(message)
19+
cipher.final
20+
end
21+
end

0 commit comments

Comments
 (0)