Skip to content
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
18 changes: 17 additions & 1 deletion csharp/core/Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
Expand All @@ -22,10 +24,14 @@ namespace AlibabaCloud.TeaUtil
public static class Common
{
private static readonly string _defaultUserAgent;
private static readonly long _processStartTime;
private static long _seqId = 0;

static Common()
{
_defaultUserAgent = GetDefaultUserAgent();
double ticks = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds;
_processStartTime = (long)(ticks * 1000);
}

public static byte[] ToBytes(string val)
Expand Down Expand Up @@ -127,7 +133,17 @@ public async static Task<byte[]> ReadAsBytesAsync(Stream stream)

public static string GetNonce()
{
return Guid.NewGuid().ToString();
int threadId = Thread.CurrentThread.ManagedThreadId;
long currentTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
long seq = Interlocked.Increment(ref seqId);
long randNum = new Random().NextInt64();
string msg = string.Format("{0}-{1}-{2}-{3}", _processStartTime, threadId, currentTime, seq, randNum);
using (MD5 md5 = MD5.Create())
{
byte[] hash = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(msg));
return BitConverter.ToString(hash).Replace("-", "").ToLower();
}
//return Guid.NewGuid().ToString();
}

public static string GetDateUTCString()
Expand Down
2 changes: 2 additions & 0 deletions csharp/tests/CommonTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ public async void Test_ReadAsBytesAsync()
public void Test_GetNonce()
{
Assert.NotNull(Common.GetNonce());
Assert.Equal(32, Common.GetNonce().length);
Assert.IsFalse(Common.GetNonce() == Common.GetNonce());
}

[Fact]
Expand Down
12 changes: 11 additions & 1 deletion php/src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
class Utils
{
private static $defaultUserAgent = '';
private static $processStartTime = intval(microtime(true) * 1000);
private static $seqId = 0;

/**
* Convert a string(utf8) to bytes.
Expand Down Expand Up @@ -111,7 +113,15 @@ public static function readAsJSON($stream)
*/
public static function getNonce()
{
return md5(uniqid() . uniqid(md5(microtime(true)), true));
$processId = getmypid();
$currentTime = intval(microtime(true) * 1000);
$seq = self::$seqId;
self::$seqId += 1;
$randNum = random_int(0, PHP_INT_MAX);
$msg = sprintf("%d-%d-%d-%d-%d", self::$processStartTime, $processId, $currentTime, $randNum);
$md5 = md5($msg);
return $md5;
// return md5(uniqid() . uniqid(md5(microtime(true)), true));
}

/**
Expand Down
18 changes: 15 additions & 3 deletions swift/Sources/TeaUtils/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import Tea


public class Client {
private static var processStartTime = Int64(Date().timeIntervalSince1970 * 1000)
private static var seqId: Int64 = 0

public static func toBytes(_ str: String?) -> [UInt8] {
str?.toBytes() ?? []
}
Expand Down Expand Up @@ -62,9 +65,18 @@ public class Client {
}

public static func getNonce() -> String {
let timestamp: TimeInterval = Date().toTimestamp()
let timestampStr: String = String(timestamp)
return (timestampStr + UUID().uuidString).md5()
let threadId = UInt64(Thread.current.threadIdentifier)
let currentTime = Int(Date().timeIntervalSince1970 * 1000)
let seq = seqId
seqId += 1
let randNum = UInt64.random(in: 0...UInt64.max)
let msg = "\(processStartTime)-\(threadId)-\(currentTime)-\(seq)-\(randNum)"
let md5 = Insecure.MD5.hash(data: msg.data(using: .utf8)!)
return md5.map { String(format: "%02hhx", $0) }.joined()

// let timestamp: TimeInterval = Date().toTimestamp()
// let timestampStr: String = String(timestamp)
// return (timestampStr + UUID().uuidString).md5()
}

public static func getDateUTCString() -> String {
Expand Down
1 change: 1 addition & 0 deletions swift/Tests/TeaUtilsTests/TeaUtilsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ final class ClientTests: XCTestCase {

func testGetNonce() {
XCTAssertEqual(32, (Client.getNonce().lengthOfBytes(using: .utf8)))
XCTAssertFalse(Client.getNonce() == Client.getNonce())
}

func testGetDateUTCString() {
Expand Down