Skip to content

Commit

Permalink
fix: Improve randomness (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
hhartzer committed Mar 7, 2024
1 parent d131d16 commit 9253dde
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 6 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ arch:
- amd64
- ppc64le
python:
- "3.5"
- "3.6"
- "3.7"
- "3.8"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Installation

To install `shortuuid` you need:

- Python 3.x.
- Python 3.6+

If you have the dependencies, you have multiple options of installation:

Expand Down
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ license = "BSD-3-Clause"
classifiers = [
"License :: OSI Approved :: BSD License",
"Programming Language :: Python",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
Expand All @@ -23,7 +22,7 @@ include = ["COPYING"]
shortuuid = "shortuuid.cli:cli"

[tool.poetry.dependencies]
python = ">=3.5"
python = ">=3.6"

[build-system]
requires = ["poetry-core"]
Expand Down
8 changes: 6 additions & 2 deletions shortuuid/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import math
import os
import uuid as _uu
import secrets
from typing import List
from typing import Optional

Expand Down Expand Up @@ -104,8 +105,11 @@ def random(self, length: Optional[int] = None) -> str:
if length is None:
length = self._length

random_num = int(binascii.b2a_hex(os.urandom(length)), 16)
return int_to_string(random_num, self._alphabet, padding=length)[:length]
random_shortuuid = ""
while len(random_shortuuid) < length:
random_shortuuid += secrets.choice(self._alphabet)

return random_shortuuid

def get_alphabet(self) -> str:
"""Return the current alphabet used for new UUIDs."""
Expand Down

0 comments on commit 9253dde

Please sign in to comment.