mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
secrets: refactor for py3 (from 3c9c04d4)
This commit is contained in:
@ -233,15 +233,23 @@ class Secret(CreatedUpdatedModel):
|
|||||||
pad_length = 16 - ((len(s) + 2) % 16)
|
pad_length = 16 - ((len(s) + 2) % 16)
|
||||||
else:
|
else:
|
||||||
pad_length = 0
|
pad_length = 0
|
||||||
return chr(len(s) >> 8) + chr(len(s) % 256) + s + os.urandom(pad_length)
|
return (
|
||||||
|
chr(len(s) >> 8).encode() +
|
||||||
|
chr(len(s) % 256).encode() +
|
||||||
|
s.encode() +
|
||||||
|
os.urandom(pad_length)
|
||||||
|
)
|
||||||
|
|
||||||
def _unpad(self, s):
|
def _unpad(self, s):
|
||||||
"""
|
"""
|
||||||
Consume the first two bytes of s as a plaintext length indicator and return only that many bytes as the
|
Consume the first two bytes of s as a plaintext length indicator and return only that many bytes as the
|
||||||
plaintext.
|
plaintext.
|
||||||
"""
|
"""
|
||||||
|
if isinstance(s[0], int):
|
||||||
|
plaintext_length = (s[0] << 8) + s[1]
|
||||||
|
elif isinstance(s[0], str):
|
||||||
plaintext_length = (ord(s[0]) << 8) + ord(s[1])
|
plaintext_length = (ord(s[0]) << 8) + ord(s[1])
|
||||||
return s[2:plaintext_length + 2]
|
return s[2:plaintext_length + 2].decode()
|
||||||
|
|
||||||
def encrypt(self, secret_key):
|
def encrypt(self, secret_key):
|
||||||
"""
|
"""
|
||||||
|
@ -100,7 +100,7 @@ class SecretTestCase(TestCase):
|
|||||||
# Ensure proper hashing algorithm is used
|
# Ensure proper hashing algorithm is used
|
||||||
hasher, iterations, salt, sha256 = s.hash.split('$')
|
hasher, iterations, salt, sha256 = s.hash.split('$')
|
||||||
self.assertEqual(hasher, 'pbkdf2_sha256', "Hashing algorithm has been modified to: {}".format(hasher))
|
self.assertEqual(hasher, 'pbkdf2_sha256', "Hashing algorithm has been modified to: {}".format(hasher))
|
||||||
self.assertGreaterEqual(iterations, 24000, "Insufficient iteration count ({}) for hash".format(iterations))
|
self.assertGreaterEqual(int(iterations), 24000, "Insufficient iteration count ({}) for hash".format(iterations))
|
||||||
self.assertGreaterEqual(len(salt), 12, "Hash salt is too short ({} chars)".format(len(salt)))
|
self.assertGreaterEqual(len(salt), 12, "Hash salt is too short ({} chars)".format(len(salt)))
|
||||||
|
|
||||||
# Test hash validation
|
# Test hash validation
|
||||||
|
Reference in New Issue
Block a user