Skip to content

Commit 40e5067

Browse files
committed
cloudvision/cvlib: Add doType7obfuscation, doSHA512Hashing
- doType7obfuscation: Implements Type 7 password obfuscation using XOR encoding with a fixed key. - doSHA512Hashing: Generates SHA-512 password hash using Unix crypt format with sanitized salt. Design Doc: https://docs.google.com/document/d/1zaZibuHoRVetrirUF8QihsS47ccvH1ulR_TvQBM0UWQ Related-Bug: BUG1316866 Change-Id: Ib008bd64c8763b653223ed6e82657aeb88344feb
1 parent 2c3d669 commit 40e5067

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

cloudvision/cvlib/utils.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
# Use of this source code is governed by the Apache License 2.0
33
# that can be found in the COPYING file.
44

5+
import re
6+
import crypt
57
from typing import Any, Dict
68
from json import loads
79

@@ -61,3 +63,44 @@ def extractJSONEncodedListArg(listArg: str):
6163
if not isinstance(extractedList, list):
6264
raise ValueError("Extracted arg must be a list")
6365
return extractedList
66+
67+
68+
def doType7obfuscation(plaintext: str, salt: int, obf: str):
69+
"""
70+
Perform Type 7 password obfuscation using XOR encoding.
71+
72+
Args:
73+
plaintext : The plaintext password to obfuscate.
74+
salt : The salt value (0-99) to use for obfuscation.
75+
obf : The obfuscator string to use for obfuscation.
76+
77+
Returns:
78+
The obfuscated password as a string.
79+
"""
80+
assert 0 <= salt < 100
81+
if not plaintext:
82+
return plaintext
83+
result = f"{salt:02d}"
84+
obfsize = len(obf)
85+
for i, x in enumerate(plaintext):
86+
y = obf[(i + salt) % obfsize]
87+
key = ord(x) ^ ord(y)
88+
result += f'{key:02X}'
89+
return result
90+
91+
92+
def doSHA512Hashing(plaintext: str, salt: str):
93+
"""
94+
Generate SHA-512 password hash using Unix crypt format.
95+
96+
Args:
97+
plaintext: The plaintext password to hash.
98+
salt: Salt string (only alphanumeric characters, periods, and slashes are used).
99+
100+
Returns:
101+
The hashed password in Unix crypt format ($6$salt$hash).
102+
"""
103+
if not plaintext:
104+
return plaintext
105+
sanitized_salt = re.sub(r'[^A-Za-z0-9\.\/]', '', salt)
106+
return crypt.crypt(plaintext, f"$6${sanitized_salt}$")

0 commit comments

Comments
 (0)