Merge pull request #537 from jinhyunr/fix_password_encoding

Fix password encrypting failure in python 2.* (#516)
This commit is contained in:
Yoon Hong
2017-01-10 10:41:39 -08:00
committed by GitHub
3 changed files with 15 additions and 4 deletions
+3
View File
@@ -267,6 +267,9 @@ def gen_password_hash(password, crypt_id, salt_len):
collection = string.ascii_letters + string.digits
salt = ''.join(random.choice(collection) for _ in range(salt_len))
salt = "${0}${1}".format(crypt_id, salt)
if sys.version_info[0] == 2:
# if python 2.*, encode to type 'str' to prevent Unicode Encode Error from crypt.crypt
password = password.encode('utf-8')
return crypt.crypt(password, salt)
+4
View File
@@ -0,0 +1,4 @@
김치
करी
hamburger
café
+8 -4
View File
@@ -23,12 +23,16 @@ from azurelinuxagent.common.future import ustr
import azurelinuxagent.common.utils.textutil as textutil
from azurelinuxagent.common.utils.textutil import Version
class TestTextUtil(AgentTestCase):
def test_get_password_hash(self):
password_hash = textutil.gen_password_hash("asdf", 6, 10)
self.assertNotEquals(None, password_hash)
password_hash = textutil.gen_password_hash("asdf", 6, 0)
self.assertNotEquals(None, password_hash)
with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'test_passwords.txt'), 'rb') as in_file:
for data in in_file:
# Remove bom on bytes data before it is converted into string.
data = textutil.remove_bom(data)
data = ustr(data, encoding='utf-8')
password_hash = textutil.gen_password_hash(data, 6, 10)
self.assertNotEquals(None, password_hash)
def test_remove_bom(self):
#Test bom could be removed