Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set key to hashid's salt for preventing others decoded your hashes #4

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/pythonapp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [ '3.5', '3.6', '3.7', '3.8' ]
python-version: [ '3.6', '3.7', '3.8', '3.9' ]
name: Python ${{ matrix.python-version }}
steps:
- uses: actions/checkout@v1
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
masonite>=2.1.0,<2.3.0
masonite>=2.1.0
pytest
coveralls
flake8
6 changes: 4 additions & 2 deletions src/masonite/contrib/essentials/helpers/views/hashid.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import os

from hashids import Hashids


def hashid(*values, decode=False, min_length=7):
hash_class = Hashids(min_length=min_length)
hash_class = Hashids(salt=os.environ.get('KEY', 'your-secret-key'), min_length=min_length)
# decode a dictionary
if type(values[0]) == dict and decode:
new_dict = {}
Expand All @@ -17,4 +19,4 @@ def hashid(*values, decode=False, min_length=7):
if not decode:
return hash_class.encode(*values)

return Hashids().decode(*values)
return hash_class.decode(*values)
12 changes: 6 additions & 6 deletions tests/feature/hashid/test_hashid.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

class MockRequest:
request_variables = {
'id': 'l9avmeG',
'id': '5XPR828',
'name': 'Joe',
}
url_params = {
'id': 'l9avmeG',
'id': '5XPR828',
}

def all(self):
Expand All @@ -19,20 +19,20 @@ def setup_method(self):
pass

def test_hashid_hashes_integer(self):
assert hashid(10) == 'l9avmeG'
assert hashid(10) == '5XPR828'

def test_hashid_hashes_several_integers(self):
assert hashid(10, 20, 30) == 'dB1I1uo'
assert hashid(10, 20, 30) == '2N5c6Hd'

def test_hashid_decodes_several_integers(self):
assert hashid('B1I1uo', decode=True) == (10,20,30)
assert hashid('2N5c6Hd', decode=True) == (10, 20, 30)

def test_hashid_decodes_non_encoded_value_is_falsey(self):
assert not hashid('B8I6ub', decode=True)

def test_hashid_can_decode_dictionary(self):
assert hashid({
'id': 'l9avmeG',
'id': '5XPR828',
'name': 'Joe',
}, decode=True) == {'id': 10, 'name': 'Joe'}

Expand Down