Skip to content

Commit

Permalink
Make new model IDs only contain alphanumeric characters. (oppia#6408)
Browse files Browse the repository at this point in the history
* Make new model IDs only contain alphanumeric characters.

* Add test for alphanumeric chars.
  • Loading branch information
seanlip authored Mar 10, 2019
1 parent 5706cf1 commit 4617d80
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
7 changes: 5 additions & 2 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,8 +424,11 @@ def convert_to_hash(input_string, max_length):
'Expected string, received %s of type %s' %
(input_string, type(input_string)))

encoded_string = base64.urlsafe_b64encode(
hashlib.sha1(input_string.encode('utf-8')).digest())
# Encodes strings using the character set [A-Za-z0-9].
encoded_string = base64.b64encode(
hashlib.sha1(input_string.encode('utf-8')).digest(),
altchars='ab'
).replace('=', 'c')

return encoded_string[:max_length]

Expand Down
1 change: 1 addition & 0 deletions utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ def test_convert_to_hash(self):
self.assertEqual(len(full_hash), 28)
self.assertEqual(len(abbreviated_hash), 5)
self.assertEqual(full_hash[:5], abbreviated_hash)
self.assertTrue(full_hash.isalnum())

def test_vfs_construct_path(self):
"""Test vfs_construct_path method."""
Expand Down

0 comments on commit 4617d80

Please sign in to comment.