123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
-
- namespace PTMedicalInsurance.APIGATEWAY_SDK
- {
- using System;
- using System.Text;
- public partial class Signer
- {
- private static char IntToHex(int n)
- {
- if (n <= 9)
- return (char)(n + (int)'0');
- else
- return (char)(n - 10 + (int)'A');
- }
- private static bool IsUrlSafeChar(char ch)
- {
- if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' || ch >= '0' && ch <= '9')
- return true;
- switch (ch)
- {
- case '-':
- case '_':
- case '.':
- case '~':
- return true;
- }
- return false;
- }
- private static byte[] UrlEncode(byte[] bytes, int offset, int count)
- {
- int cUnsafe = 0;
-
- for (int i = 0; i < count; i++)
- {
- char ch = (char)bytes[offset + i];
- if (!IsUrlSafeChar(ch))
- cUnsafe++;
- }
-
- if (cUnsafe == 0)
- {
-
- if (0 == offset && bytes.Length == count)
- {
- return bytes;
- }
- else
- {
- var subarray = new byte[count];
- Buffer.BlockCopy(bytes, offset, subarray, 0, count);
- return subarray;
- }
- }
-
- byte[] expandedBytes = new byte[count + cUnsafe * 2];
- int pos = 0;
- for (int i = 0; i < count; i++)
- {
- byte b = bytes[offset + i];
- char ch = (char)b;
- if (IsUrlSafeChar(ch))
- {
- expandedBytes[pos++] = b;
- }
- else
- {
- expandedBytes[pos++] = (byte)'%';
- expandedBytes[pos++] = (byte)IntToHex((b >> 4) & 0xf);
- expandedBytes[pos++] = (byte)IntToHex(b & 0x0f);
- }
- }
- return expandedBytes;
- }
- private static string UrlEncode(string value)
- {
- if (value == null)
- return null;
- byte[] bytes = Encoding.UTF8.GetBytes(value);
- return Encoding.UTF8.GetString(UrlEncode(bytes, 0, bytes.Length));
- }
- }
- }
|