Skip to content

Instantly share code, notes, and snippets.

@fengjing
Forked from wonderbeyond/get_guid.py
Created August 2, 2024 07:56
Show Gist options
  • Save fengjing/d634c0f9fd02c34f00433e4f45b7f740 to your computer and use it in GitHub Desktop.
Save fengjing/d634c0f9fd02c34f00433e4f45b7f740 to your computer and use it in GitHub Desktop.

Revisions

  1. @wonderbeyond wonderbeyond revised this gist Sep 16, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion get_random_string.py
    Original file line number Diff line number Diff line change
    @@ -28,5 +28,5 @@ def get_random_string(length=12,
    import random


    def get_random_string(N=12, allowed_chars=(string.letters + string.digits)):
    def get_random_string(N=12, allowed_chars=(string.ascii_letters + string.digits)):
    return ''.join(random.choice(allowed_chars) for _ in range(N))
  2. @wonderbeyond wonderbeyond revised this gist May 10, 2018. 1 changed file with 11 additions and 1 deletion.
    12 changes: 11 additions & 1 deletion get_random_string.py
    Original file line number Diff line number Diff line change
    @@ -19,4 +19,14 @@ def get_random_string(length=12,
    time.time(),
    'O_O-SECRET_KEY')).encode('utf-8')
    ).digest())
    return ''.join(random.choice(allowed_chars) for i in range(length))
    return ''.join(random.choice(allowed_chars) for i in range(length))


    # --- Simple One ---

    import string
    import random


    def get_random_string(N=12, allowed_chars=(string.letters + string.digits)):
    return ''.join(random.choice(allowed_chars) for _ in range(N))
  3. @wonderbeyond wonderbeyond revised this gist Nov 20, 2017. 1 changed file with 9 additions and 2 deletions.
    11 changes: 9 additions & 2 deletions get_guid.py
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,18 @@
    import os
    import binascii
    import uuid
    from xid import Xid


    def get_guid():
    def get_guid(style='uuid'):
    """Get a globally unique string for identify things"""
    return uuid.uuid4().hex
    if style == 'uuid':
    return uuid.uuid4().hex

    if style == 'xid':
    return Xid().string()

    raise RuntimeError('Unsupported guid style: {0}'.format(style))


    # Equivalent implementation
  4. @wonderbeyond wonderbeyond revised this gist Nov 16, 2017. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions get_guid.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,13 @@
    import os
    import binascii
    import uuid


    def get_guid():
    """Get a globally unique string for identify things"""
    return uuid.uuid4().hex


    # Equivalent implementation
    def get_guid():
    return binascii.hexlify(os.urandom(16)).decode()
  5. @wonderbeyond wonderbeyond revised this gist Nov 16, 2017. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions get_guid.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    import uuid


    def get_guid():
    """Get a globally unique string for identify things"""
    return uuid.uuid4().hex
  6. @wonderbeyond wonderbeyond revised this gist Nov 16, 2017. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions get_random_string.py
    Original file line number Diff line number Diff line change
    @@ -10,6 +10,7 @@ def get_random_string(length=12,
    See also:
    https://github.com/rs/xid (globally unique id generator)
    https://stackoverflow.com/questions/41354205/how-to-generate-a-unique-auth-token-in-python
    https://docs.python.org/3/whatsnew/3.6.html#secrets
    """
    random.seed(
    hashlib.sha256(
  7. @wonderbeyond wonderbeyond revised this gist Nov 16, 2017. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion get_random_string.py
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,9 @@ def get_random_string(length=12,
    allowed_chars='abcdefghijklmnopqrstuvwxyz'
    'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'):
    """
    See also: https://github.com/rs/xid (globally unique id generator)
    See also:
    https://github.com/rs/xid (globally unique id generator)
    https://stackoverflow.com/questions/41354205/how-to-generate-a-unique-auth-token-in-python
    """
    random.seed(
    hashlib.sha256(
  8. @wonderbeyond wonderbeyond revised this gist Oct 17, 2017. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions get_random_string.py
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,9 @@
    def get_random_string(length=12,
    allowed_chars='abcdefghijklmnopqrstuvwxyz'
    'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'):
    """
    See also: https://github.com/rs/xid (globally unique id generator)
    """
    random.seed(
    hashlib.sha256(
    ("%s%s%s" % (
  9. @wonderbeyond wonderbeyond created this gist Nov 29, 2016.
    16 changes: 16 additions & 0 deletions get_random_string.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    import random
    import hashlib
    import time


    def get_random_string(length=12,
    allowed_chars='abcdefghijklmnopqrstuvwxyz'
    'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'):
    random.seed(
    hashlib.sha256(
    ("%s%s%s" % (
    random.getstate(),
    time.time(),
    'O_O-SECRET_KEY')).encode('utf-8')
    ).digest())
    return ''.join(random.choice(allowed_chars) for i in range(length))