mirror of
https://github.com/netchx/netch.git
synced 2026-03-14 17:43:18 +08:00
Merge pull request #838 from H1JK/uuid-generator
Support Xray UUIDv5 mapping standard
This commit is contained in:
@@ -83,7 +83,7 @@ public static class V2rayConfigUtils
|
||||
{
|
||||
new User
|
||||
{
|
||||
id = vless.UserID,
|
||||
id = getUUID(vless.UserID),
|
||||
flow = vless.Flow.ValueOrDefault(),
|
||||
encryption = vless.EncryptMethod
|
||||
}
|
||||
@@ -126,7 +126,7 @@ public static class V2rayConfigUtils
|
||||
{
|
||||
new User
|
||||
{
|
||||
id = vmess.UserID,
|
||||
id = getUUID(vmess.UserID),
|
||||
alterId = vmess.AlterID,
|
||||
security = vmess.EncryptMethod
|
||||
}
|
||||
@@ -367,4 +367,13 @@ public static class V2rayConfigUtils
|
||||
|
||||
return streamSettings;
|
||||
}
|
||||
|
||||
public static string getUUID(string uuid)
|
||||
{
|
||||
if (uuid.Length == 36 || uuid.Length == 32)
|
||||
{
|
||||
return uuid;
|
||||
}
|
||||
return uuid.GenerateUUIDv5();
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Text;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace Netch.Utils;
|
||||
|
||||
@@ -78,4 +79,30 @@ public static class StringExtension
|
||||
{
|
||||
return !string.IsNullOrWhiteSpace(value) ? value.Split(',') : default;
|
||||
}
|
||||
|
||||
public static string GenerateUUIDv5(this string str)
|
||||
{
|
||||
// https://github.com/XTLS/Xray-core/discussions/715
|
||||
// https://xray-uuid.ducksoft.site/
|
||||
|
||||
SHA1 sha1 = new SHA1CryptoServiceProvider();
|
||||
|
||||
// example string: "example"
|
||||
List<byte> byteSource = new List<byte>();
|
||||
byteSource.AddRange(new byte[16]);
|
||||
byteSource.AddRange(Encoding.UTF8.GetBytes(str));
|
||||
|
||||
byte[] Sha1Bytes = sha1.ComputeHash(byteSource.ToArray()).Skip(0).Take(16).ToArray();
|
||||
sha1.Dispose();
|
||||
|
||||
//UUIDv5: [254 181 68 49 48 27 82 187 166 221 225 233 62 129 187 158]
|
||||
|
||||
Sha1Bytes[6] = (byte)((Sha1Bytes[6] & 0x0f) | (5 << 4));
|
||||
Sha1Bytes[8] = (byte)(Sha1Bytes[8] & (0xff >> 2) | (0x02 << 6));
|
||||
|
||||
return BitConverter.ToString(Sha1Bytes).Replace("-", "")
|
||||
.Insert(8, "-").Insert(13, "-").Insert(18, "-").Insert(23, "-")
|
||||
.ToLower();
|
||||
//UUIDv5: feb54431-301b-52bb-a6dd-e1e93e81bb9e
|
||||
}
|
||||
}
|
||||
@@ -24,7 +24,7 @@ namespace Tests
|
||||
var str = "example";
|
||||
|
||||
SHA1 sha1 = new SHA1CryptoServiceProvider();
|
||||
byte[] StrBytes = Encoding.Default.GetBytes(str);
|
||||
byte[] StrBytes = Encoding.UTF8.GetBytes(str);
|
||||
|
||||
List<byte> byteSource = new List<byte>();
|
||||
byteSource.AddRange(bytes);
|
||||
|
||||
Reference in New Issue
Block a user