首次推送

This commit is contained in:
lym
2026-03-09 00:13:46 +08:00
commit b19613bb31
13974 changed files with 2767735 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Text;
/**
* Copyright (c) 2018 Tencent Cloud. All rights reserved.
* 11/1/2018 9:48:38 PM
* bradyxiao
*/
namespace COSXML.Utils
{
public sealed class CosValueAttribute : Attribute
{
public string Value
{
get;
private set;
}
public CosValueAttribute(string value)
{
this.Value = value;
}
}
}

120
COSXML/Utils/DigestUtils.cs Normal file
View File

@@ -0,0 +1,120 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
/**
* Copyright (c) 2018 Tencent Cloud. All rights reserved.
* 11/9/2018 9:53:39 PM
* bradyxiao
*/
namespace COSXML.Utils
{
public sealed class DigestUtils
{
public static string GetSha1ToHexString(string content, Encoding encode)
{
return GetSha1ToHexString(encode.GetBytes(content));
}
public static string GetSha1ToHexString(byte[] content)
{
SHA1 sha1 = new SHA1CryptoServiceProvider();
byte[] result = sha1.ComputeHash(content);
sha1.Clear();
var hexStr = new StringBuilder();
foreach( byte b in result)
{
hexStr.Append(b.ToString("x2")); // to lower
}
return hexStr.ToString();
}
public static string GetSha1ToHexString(Stream inputStream)
{
SHA1 sha1 = new SHA1CryptoServiceProvider();
byte[] result = sha1.ComputeHash(inputStream);
sha1.Clear();
var hexStr = new StringBuilder();
foreach (byte b in result)
{
hexStr.Append(b.ToString("x2")); // to lower
}
return hexStr.ToString();
}
public static string GetHamcSha1ToHexString(string content, Encoding contentEncoding, string key, Encoding keyEncoding)
{
HMACSHA1 hmacSha1 = new HMACSHA1(keyEncoding.GetBytes(key));
byte[] result = hmacSha1.ComputeHash(contentEncoding.GetBytes(content));
hmacSha1.Clear();
var hexStr = new StringBuilder();
foreach (byte b in result)
{
hexStr.Append(b.ToString("x2")); // to lower
}
return hexStr.ToString();
}
public static string GetHamcSha1ToBase64(string content, Encoding contentEncoding, string key, Encoding keyEncoding)
{
HMACSHA1 hmacSha1 = new HMACSHA1(keyEncoding.GetBytes(key));
byte[] result = hmacSha1.ComputeHash(contentEncoding.GetBytes(content));
hmacSha1.Clear();
return Convert.ToBase64String(result);
}
public static string GetMd5ToBase64(string content, Encoding encoding)
{
return GetMd5ToBase64(encoding.GetBytes(content));
}
public static string GetMd5ToBase64(byte[] content)
{
MD5 md5 = MD5.Create();
byte[] result = md5.ComputeHash(content);
md5.Clear();
return Convert.ToBase64String(result);
}
public static string GetMd5ToBase64(Stream inStream)
{
MD5 md5 = MD5.Create();
byte[] result = md5.ComputeHash(inStream);
md5.Clear();
return Convert.ToBase64String(result);
}
public static string GetMd5ToBase64(Stream inStream, long size)
{
int bufferSize = 1024 * 16;
byte[] buffer = new byte[bufferSize];
int readLength = 0;
long total = 0L;
byte[] data = new byte[bufferSize];
MD5 md5 = MD5.Create();
int count = (int)(size - total);
while ((readLength = inStream.Read(buffer, 0, count > buffer.Length ? buffer.Length : count)) > 0)
{
md5.TransformBlock(buffer, 0, readLength, data, 0);
total += readLength;
count = (int)(size - total);
if (count <= 0) break;
}
md5.TransformFinalBlock(buffer, 0, 0);
string result = Convert.ToBase64String(md5.Hash);
md5.Clear();
return result;
}
public static string GetBase64(string content, Encoding encoding)
{
byte[] result = encoding.GetBytes(content);
return Convert.ToBase64String(result);
}
}
}

26
COSXML/Utils/EnumUtils.cs Normal file
View File

@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Text;
/**
* Copyright (c) 2018 Tencent Cloud. All rights reserved.
* 11/1/2018 8:51:37 PM
* bradyxiao
*/
namespace COSXML.Utils
{
public static class EnumUtils
{
public static string GetValue(Enum value)
{
if (value == null)
{
return null;
}
string name = value.ToString();
var fieldInfo = value.GetType().GetField(name);
var attributes = fieldInfo.GetCustomAttributes(typeof(CosValueAttribute), false);
return attributes != null && attributes.Length > 0 ? ((CosValueAttribute)attributes[0]).Value : name;
}
}
}

View File

@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Text;
/**
* Copyright (c) 2018 Tencent Cloud. All rights reserved.
* 11/6/2018 10:58:01 AM
* bradyxiao
*/
namespace COSXML.Utils
{
public sealed class HttpDateTimeUtils
{
}
}

View File

@@ -0,0 +1,96 @@
using System;
using System.Collections.Generic;
using System.Text;
using COSXML.CosException;
using COSXML.Common;
/**
* Copyright (c) 2018 Tencent Cloud. All rights reserved.
* 11/23/2018 2:57:33 PM
* bradyxiao
*/
namespace COSXML.Utils
{
public sealed class StringUtils
{
public static int Compare(string strA, string strB, bool ignoreCase)
{
if (strA == null || strB == null) throw new CosClientException((int)CosClientError.INVALID_ARGUMENT, "strA = null or strA = null");
if (ignoreCase)
{
strA = strA.ToLower();
strB = strB.ToLower();
}
int strALen = strA.Length;
int strBLen = strB.Length;
for (int i = 0, size = strALen > strBLen ? strBLen : strALen; i < size; i++)
{
int temp1 = (int)strA[i];
int temp2 = (int)strB[i];
if (temp1 > temp2)
{
return 1;
}
else if (temp1 < temp2)
{
return -1;
}
}
if (strALen > strBLen) return 1;
if (strALen < strBLen) return -1;
return 0;
}
public static Dictionary<string, string> ParseURL(string url)
{
Dictionary<string, string> urlTuple = new Dictionary<string, string>();
if (String.IsNullOrEmpty(url)) return null;
int index = url.IndexOf("://");
if (index > 0)
{
urlTuple.Add("Scheme", url.Substring(0, index));
}
else
{
throw new ArgumentException("url need start with http:// or https://");
}
int tmp = index;
index = url.IndexOf('/', tmp + 3);
if (index > 0)
{
urlTuple.Add("Host", url.Substring(tmp + 3, index - tmp - 3));
tmp = index;
}
else
{
urlTuple.Add("Host", url.Substring(tmp + 3));
return urlTuple;
}
index = url.IndexOf('?', tmp);
if (index > 0)
{
urlTuple.Add("Path", url.Substring(tmp, index - tmp));
tmp = index;
}
else
{
urlTuple.Add("Path", url.Substring(tmp));
return urlTuple;
}
index = url.IndexOf("#", tmp + 1);
if (index > 0)
{
urlTuple.Add("Query", url.Substring(tmp + 1, index - tmp - 1));
tmp = index;
}
else
{
urlTuple.Add("Query", url.Substring(tmp + 1));
return urlTuple;
}
urlTuple.Add("Fragment", url.Substring(tmp + 1));
return urlTuple;
}
}
}

83
COSXML/Utils/TimeUtils.cs Normal file
View File

@@ -0,0 +1,83 @@
using System;
using System.Collections.Generic;
using System.Text;
/**
* Copyright (c) 2018 Tencent Cloud. All rights reserved.
* 11/6/2018 10:03:00 AM
* bradyxiao
*/
namespace COSXML.Utils
{
public sealed class TimeUtils
{
// utc start time
public static readonly DateTime UTC_START_TIME = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
/**
* 根据UTC时间戳的含义= UTC时间 - UTC起始时间
*/
public static long GetCurrentTime(TimeUnit timeUnit)
{
TimeSpan timeSpan = DateTime.UtcNow - UTC_START_TIME;
long result = -1L;
switch (timeUnit)
{
case TimeUnit.DAYS:
result = (long)timeSpan.TotalDays;
break;
case TimeUnit.HOURS:
result = (long)timeSpan.TotalHours;
break;
case TimeUnit.MINUTES:
result = (long)timeSpan.TotalMinutes;
break;
case TimeUnit.SECONDS:
result = (long)timeSpan.TotalSeconds;
break;
case TimeUnit.MILLISECONDS:
result = (long)timeSpan.TotalMilliseconds;
break;
}
return result;
}
/**
* 先获取 对应的 UTC -> 转为 当前时区的时间
*/
public static string GetFormatTime(string format, long time, TimeUnit timeUnit)
{
DateTime end = DateTime.MinValue;
DateTime start = UTC_START_TIME;
switch (timeUnit)
{
case TimeUnit.DAYS:
end = start.AddDays(time);
break;
case TimeUnit.HOURS:
end = start.AddHours(time);
break;
case TimeUnit.MINUTES:
end = start.AddMinutes(time);
break;
case TimeUnit.SECONDS:
end = start.AddSeconds(time);
break;
case TimeUnit.MILLISECONDS:
end = start.AddMilliseconds(time);
break;
}
end = TimeZone.CurrentTimeZone.ToLocalTime(end);
return end.ToString(format);
}
}
public enum TimeUnit
{
MILLISECONDS = 0,
SECONDS,
MINUTES,
HOURS,
DAYS,
}
}

View File

@@ -0,0 +1,101 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
/**
* Copyright (c) 2018 Tencent Cloud. All rights reserved.
* 11/5/2018 5:40:47 PM
* bradyxiao
*/
namespace COSXML.Utils
{
/**
* URL Encode:
* 可分为绝对不需要编码的字符:[a~z][A~A][0~9][._-~]
* 特殊字符需要视情况而言:!*'();:@&=+$,/?#[]
* 空字符用+或者%20代替
* 汉字绝地需要编码
* 因此针对完整URL则要分块进行编码path块query块fragment块host块
* path块需要以'/'进行分割,即 '/' 不需要编码,其它均需要编码
* query块需要以'&'分割一对对key=valuekey 和 value 需要编码, "="不编码
* fragment全部进行编码
* URL Decode:
* 不需要判断是否是特殊字符,因为其解码规则很简单,直接根据内容中是否出现%来判断是否需要解码,
* 还是直接输出:若出现了%,则连续读出其后两位进行解码;反之,直接输出
*/
public sealed class URLEncodeUtils
{
//只有字母和数字[0-9a-zA-Z]、一些特殊符号"-_.!*~',以及某些保留字才可以不经过编码直接用于URL。"
public const string URLAllowChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~";
/// <summary>
/// 针对 URL 中 path 编码,则需要先将其按照 '/'分割,然后进行逐个块进行 value 编码
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static string EncodePathOfURL(string path)
{
if (String.IsNullOrEmpty(path)) return String.Empty;
char separator = '/';
int start = 0, length = path.Length;
int index = path.IndexOf(separator, start);
StringBuilder result = new StringBuilder();
while (index != -1 && start < length)
{
if (start != index)
{
result.Append(Encode(path.Substring(start, index - start)));
}
result.Append(separator);
start = index + 1;
index = path.IndexOf(separator, start);
}
if(start < length) result.Append(Encode(path.Substring(start)));
return result.ToString();
}
public static string Encode(string value)
{
return Encode(value, Encoding.UTF8);
}
/// <summary>
/// 针对单个value则只需满足 URLAllowChars 不需要编码即可.
/// urlEncode: 转为一个byte -> 转为两个16进制 -> 前面加上 %
/// </summary>
/// <param name="value"></param>
/// <param name="encoding"></param>
/// <returns></returns>
public static string Encode(string value, Encoding encoding)
{
if (String.IsNullOrEmpty(value)) return String.Empty;
StringBuilder result = new StringBuilder(value.Length * 2); // %xy%xy
byte[] strToBytes = encoding.GetBytes(value);
foreach(byte b in strToBytes)
{
char ch = (char)b;
if(URLAllowChars.IndexOf(ch) != -1)
{
result.Append(ch);
}
else
{
result.Append('%').Append(String.Format(CultureInfo.InvariantCulture, "{0:X2}", (int)b));
}
}
return result.ToString();
}
/// <summary>
/// 解码比较统一,此处借用 Uri 来实现
/// </summary>
/// <param name="valueEncode"></param>
/// <returns></returns>
public static string Decode(string valueEncode)
{
if (String.IsNullOrEmpty(valueEncode)) return String.Empty;
valueEncode.Replace('+', ' ');
return Uri.UnescapeDataString(valueEncode);
}
}
}