Skip to content

Commit

Permalink
fix: improve the design of nonce
Browse files Browse the repository at this point in the history
  • Loading branch information
h-hg authored and JacksonTian committed Sep 6, 2023
1 parent fbc05ef commit 7e03ac9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
19 changes: 15 additions & 4 deletions aliyun-python-sdk-core/aliyunsdkcore/utils/parameter_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import socket
import uuid
import time
import random
import threading
import xml.etree.cElementTree as EleTree

from aliyunsdkcore.compat import ensure_bytes, ensure_string
Expand All @@ -30,12 +32,21 @@
FORMAT_ISO_8601 = "%Y-%m-%dT%H:%M:%SZ"
FORMAT_RFC_2616 = "%a, %d %b %Y %H:%M:%S GMT"

_process_start_time = int(time.time() * 1000)
_seqId = 0

def get_uuid():
name = socket.gethostname() + str(uuid.uuid1())
namespace = uuid.NAMESPACE_URL
return str(uuid.uuid5(namespace, name))

def get_uuid():
global _seqId
thread_id = threading.current_thread().ident
current_time = int(time.time() * 1000)
seq = _seqId
_seqId += 1
randNum = random.getrandbits(64)
msg = '%d-%d-%d-%d-%d' % (_process_start_time, thread_id, current_time, seq, randNum)
md5 = hashlib.md5()
md5.update(msg.encode('utf-8'))
return md5.hexdigest()

def get_iso_8061_date():
return time.strftime(FORMAT_ISO_8601, time.gmtime())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class TestShaHmac1(unittest.TestCase):
def test_get_uuid(self):
uuid = helper.get_uuid()
self.assertEqual(36, len(uuid))
self.assertEqual(32, len(uuid))
self.assertNotEqual(helper.get_uuid(), helper.get_uuid())

def test_md5_sum(self):
Expand Down

0 comments on commit 7e03ac9

Please sign in to comment.