public string GetSignature(string url) { Dictionary <string, string> dic = new Dictionary <string, string>(); dic.Add("noncestr", nonceStr); dic.Add("jsapi_ticket", WechatApi.GetAccessTokenAndRefresh()); dic.Add("timestamp", timestamp); dic.Add("url", url); Dictionary <string, string> sortDic = dic.OrderBy(e => e.Key).ToDictionary(k => k.Key, v => v.Value); StringBuilder sb = new StringBuilder(); foreach (KeyValuePair <string, string> pair in sortDic) { sb.Append(pair.Key); sb.Append("="); sb.Append(pair.Value); sb.Append("&"); } sb.Remove(sb.Length - 1, 1); string sign = StringCoding.EncodeSHA1(sb.ToString()).ToLower(); LogHelper.Info(String.Format("WxPayJsConfig GetSignature:{0} -> {1}", sb.ToString(), sign)); return(sign); }
/// <summary> /// Returns a <see cref="String"/> instance read asynchronously from the <paramref name="stream"/>. /// </summary> /// <param name="stream">The extended <see cref="Stream"/> instance.</param> /// <param name="coding">The <see cref="StringCoding"/> determining how the length of the string is /// stored.</param> /// <param name="encoding">The <see cref="Encoding"/> to parse the bytes with, or <c>null</c> to use /// <see cref="Encoding.UTF8"/>.</param> /// <param name="converter">The <see cref="ByteConverter"/> to use for converting multibyte data.</param> /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> /// <returns>The value read from the current stream.</returns> public static async Task <String> ReadStringAsync(this Stream stream, StringCoding coding = StringCoding.VariableByteCount, Encoding encoding = null, ByteConverter converter = null, CancellationToken cancellationToken = default(CancellationToken)) { encoding = encoding ?? Encoding.UTF8; converter = converter ?? ByteConverter.System; switch (coding) { case StringCoding.VariableByteCount: return(await ReadStringWithLengthAsync(stream, stream.Read7BitInt32(), false, encoding, cancellationToken)); case StringCoding.ByteCharCount: return(await ReadStringWithLengthAsync(stream, stream.ReadByte(), true, encoding, cancellationToken)); case StringCoding.Int16CharCount: return(await ReadStringWithLengthAsync(stream, ReadInt16(stream, converter), true, encoding, cancellationToken)); case StringCoding.Int32CharCount: return(await ReadStringWithLengthAsync(stream, ReadInt32(stream, converter), true, encoding, cancellationToken)); case StringCoding.ZeroTerminated: return(await ReadStringZeroPostfixAsync(stream, encoding, cancellationToken)); default: throw new ArgumentException($"Invalid {nameof(StringCoding)}.", nameof(coding)); } }
private string Sign() { Dictionary <string, string> dic = new Dictionary <string, string>(); dic.Add("appId", appId); dic.Add("timeStamp", timestamp); dic.Add("nonceStr", nonceStr); dic.Add("package", package); dic.Add("signType", signType); Dictionary <string, string> sortDic = dic.OrderBy(e => e.Key).ToDictionary(k => k.Key, v => v.Value); StringBuilder sb = new StringBuilder(); foreach (KeyValuePair <string, string> pair in sortDic) { sb.Append(pair.Key); sb.Append("="); sb.Append(pair.Value); sb.Append("&"); } sb.Append("key="); sb.Append(ConfigurationManager.AppSettings["WechatPayApiSecret"]); string sign = StringCoding.EncodeMD5(sb.ToString()).ToUpper(); LogHelper.Info(String.Format("WxPayRequest Sign :{0} -> {1}", sb.ToString(), sign)); return(sign); }
public static void Test() { Dictionary <string, string> dic = new Dictionary <string, string>(); dic.Add("jsapi_ticket", "sM4AOVdWfPE4DxkXGEs8VMCPGGVi4C3VM0P37wVUCFvkVAy_90u5h9nbSlYy3-Sl-HhTdfl2fzFy1AOcHKP7qg"); dic.Add("timestamp", "1414587457"); dic.Add("url", "http://mp.weixin.qq.com?params=value"); dic.Add("noncestr", "Wm3WZYTPz0wzccnW"); Dictionary <string, string> sortDic = dic.OrderBy(o => o.Key).ToDictionary(o => o.Key, p => p.Value); StringBuilder sb = new StringBuilder(); foreach (KeyValuePair <string, string> pair in sortDic) { sb.Append(pair.Key); sb.Append("="); sb.Append(pair.Value); sb.Append("&"); } sb.Remove(sb.Length - 1, 1); Console.WriteLine(sb.ToString()); string codeString = StringCoding.EncodeSHA1(sb.ToString()); Console.WriteLine(codeString); }
/// <summary> /// Returns an array of <see cref="String"/> instances read asynchronously from the <paramref name="stream"/>. /// </summary> /// <param name="stream">The extended <see cref="Stream"/> instance.</param> /// <param name="count">The number of values to read.</param> /// <param name="coding">The <see cref="StringCoding"/> determining how the length of the strings is /// stored.</param> /// <param name="encoding">The <see cref="Encoding"/> to parse the bytes with, or <c>null</c> to use /// <see cref="Encoding.UTF8"/>.</param> /// <param name="converter">The <see cref="ByteConverter"/> to use for converting multibyte data.</param> /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> /// <returns>The array of values read from the current stream.</returns> public static async Task <String[]> ReadStringsAsync(this Stream stream, int count, StringCoding coding = StringCoding.VariableByteCount, Encoding encoding = null, ByteConverter converter = null, CancellationToken cancellationToken = default(CancellationToken)) { converter = converter ?? ByteConverter.System; return(await ReadManyAsync(stream, count, () => ReadStringAsync(stream, coding, encoding, converter, cancellationToken))); }
/// <summary> /// Returns an array of <see cref="String"/> instances read from the <paramref name="stream"/>. /// </summary> /// <param name="stream">The extended <see cref="Stream"/> instance.</param> /// <param name="count">The number of values to read.</param> /// <param name="coding">The <see cref="StringCoding"/> determining how the length of the strings is /// stored.</param> /// <param name="encoding">The <see cref="Encoding"/> to parse the bytes with, or <c>null</c> to use /// <see cref="Encoding.UTF8"/>.</param> /// <param name="converter">The <see cref="ByteConverter"/> to use for converting multibyte data.</param> /// <returns>The array of values read from the current stream.</returns> public static String[] ReadStrings(this Stream stream, int count, StringCoding coding = StringCoding.VariableByteCount, Encoding encoding = null, ByteConverter converter = null) { converter = converter ?? ByteConverter.System; return(ReadMany(stream, count, () => ReadString(stream, coding, encoding, converter))); }
public static void Test() { string sourceString = "中国"; Console.WriteLine(StringCoding.EncodeMD5(sourceString)); Console.WriteLine(StringCoding.EncodeMD5Encrypt16(sourceString)); Console.WriteLine(StringCoding.EncodeMD5Encrypt32(sourceString)); Console.Read(); }
private void buttonHmacsha1_Click(object sender, EventArgs e) { string key = textBoxHmacsha1Key.Text; if (string.IsNullOrEmpty(key)) { key = DateTime.Now.ToString(); } richTextBoxOut.Text = StringCoding.EncodeHmacsha1(key, richTextBoxIn.Text); }
/// <summary> /// Writes an enumerable of <see cref="String"/> values to the <paramref name="stream"/>. /// </summary> /// <param name="stream">The extended <see cref="Stream"/> instance.</param> /// <param name="values">The values to write.</param> /// <param name="coding">The <see cref="StringCoding"/> determining how the length of the strings is /// stored.</param> /// <param name="encoding">The <see cref="Encoding"/> to parse the bytes with, or <c>null</c> to use /// <see cref="Encoding.UTF8"/>.</param> /// <param name="converter">The <see cref="ByteConverter"/> to use for converting multibyte data.</param> public static void Write(this Stream stream, IEnumerable <String> values, StringCoding coding = StringCoding.VariableByteCount, Encoding encoding = null, ByteConverter converter = null) { encoding = encoding ?? Encoding.UTF8; converter = converter ?? ByteConverter.System; foreach (var value in values) { Write(stream, value, coding, encoding, converter); } }
/// <summary> /// Writes an enumerable of <see cref="String"/> values asynchronously to the <paramref name="stream"/>. /// </summary> /// <param name="stream">The extended <see cref="Stream"/> instance.</param> /// <param name="values">The values to write.</param> /// <param name="coding">The <see cref="StringCoding"/> determining how the length of the strings is /// stored.</param> /// <param name="encoding">The <see cref="Encoding"/> to parse the bytes with, or <c>null</c> to use /// <see cref="Encoding.UTF8"/>.</param> /// <param name="converter">The <see cref="ByteConverter"/> to use for converting multibyte data.</param> /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> public static async Task WriteAsync(this Stream stream, IEnumerable <String> values, StringCoding coding = StringCoding.VariableByteCount, Encoding encoding = null, ByteConverter converter = null, CancellationToken cancellationToken = default(CancellationToken)) { encoding = encoding ?? Encoding.UTF8; converter = converter ?? ByteConverter.System; foreach (var value in values) { await WriteAsync(stream, value, coding, encoding, converter, cancellationToken); } }
/// <summary> /// 对时间戳和随机数签名 /// </summary> /// <param name="timestamp"></param> /// <param name="nonce"></param> /// <returns></returns> public static string Sign(string timestamp, string nonce) { string token = ConfigurationManager.AppSettings["WechatToken"]; string[] tmpArr = { token, timestamp, nonce }; Array.Sort(tmpArr); string tmpStr = string.Join("", tmpArr); return(StringCoding.EncodeSHA1(tmpStr).ToLower()); }
// ---- CONSTRUCTORS ------------------------------------------------------------------------------------------- /// <summary> /// Initializes a new instance of the <see cref="BinaryStream"/> class with the given default configuration. /// </summary> /// <param name="baseStream">The output stream.</param> /// <param name="converter">The <see cref="ByteConverter"/> to use. Defaults to /// <see cref="ByteConverter.System"/>.</param> /// <param name="encoding">The character encoding to use. Defaults to <see cref="Encoding.UTF8"/>.</param> /// <param name="booleanCoding">The <see cref="BinaryData.BooleanCoding"/> data format to use for /// <see cref="Boolean"/> values.</param> /// <param name="dateTimeCoding">The <see cref="BinaryData.DateTimeCoding"/> data format to use for /// <see cref="DateTime"/> values.</param> /// <param name="stringCoding">The <see cref="BinaryData.StringCoding"/> data format to use for /// <see cref="String"/> values.</param> /// <param name="leaveOpen"><c>true</c> to leave the base stream open after the <see cref="BinaryStream"/> /// object is disposed; otherwise <c>false</c>.</param> /// <exception cref="ArgumentException">The stream does not support writing or is already closed.</exception> /// <exception cref="ArgumentNullException">output is null.</exception> public BinaryStream(Stream baseStream, ByteConverter converter = null, Encoding encoding = null, BooleanCoding booleanCoding = BooleanCoding.Byte, DateTimeCoding dateTimeCoding = DateTimeCoding.NetTicks, StringCoding stringCoding = StringCoding.VariableByteCount, bool leaveOpen = false) { BaseStream = baseStream; ByteConverter = converter; Encoding = encoding; BooleanCoding = booleanCoding; DateTimeCoding = dateTimeCoding; StringCoding = stringCoding; _leaveOpen = leaveOpen; }
// ---- Write ---- /// <summary> /// Writes a <see cref="String"/> value to the <paramref name="stream"/>. /// </summary> /// <param name="stream">The extended <see cref="Stream"/> instance.</param> /// <param name="value">The value to write.</param> /// <param name="coding">The <see cref="StringCoding"/> determining how the length of the string is /// stored.</param> /// <param name="encoding">The <see cref="Encoding"/> to parse the bytes with, or <c>null</c> to use /// <see cref="Encoding.UTF8"/>.</param> /// <param name="converter">The <see cref="ByteConverter"/> to use for converting multibyte data.</param> public static void Write(this Stream stream, String value, StringCoding coding = StringCoding.VariableByteCount, Encoding encoding = null, ByteConverter converter = null) { encoding = encoding ?? Encoding.UTF8; converter = converter ?? ByteConverter.System; byte[] textBuffer = encoding.GetBytes(value); switch (coding) { case StringCoding.VariableByteCount: Write7BitInt32(stream, textBuffer.Length); stream.Write(textBuffer, 0, textBuffer.Length); break; case StringCoding.ByteCharCount: stream.WriteByte((byte)value.Length); stream.Write(textBuffer, 0, textBuffer.Length); break; case StringCoding.Int16CharCount: converter.GetBytes((Int16)value.Length, Buffer, 0); stream.Write(Buffer, 0, sizeof(Int16)); stream.Write(textBuffer, 0, textBuffer.Length); break; case StringCoding.Int32CharCount: converter.GetBytes(value.Length, Buffer, 0); stream.Write(Buffer, 0, sizeof(Int32)); stream.Write(textBuffer, 0, textBuffer.Length); break; case StringCoding.ZeroTerminated: stream.Write(textBuffer, 0, textBuffer.Length); switch (encoding.GetByteCount("A")) { case sizeof(Byte): stream.WriteByte(0); break; case sizeof(Int16): stream.WriteByte(0); stream.WriteByte(0); break; } break; case StringCoding.Raw: stream.Write(textBuffer, 0, textBuffer.Length); break; default: throw new ArgumentException($"Invalid {nameof(StringCoding)}.", nameof(coding)); } }
/// <summary> /// Writes an enumerable of <see cref="String"/> values asynchronously to the <paramref name="stream"/>. /// </summary> /// <param name="stream">The extended <see cref="Stream"/> instance.</param> /// <param name="values">The values to write.</param> /// <param name="coding">The <see cref="StringCoding"/> determining how the length of the strings is /// stored.</param> /// <param name="encoding">The <see cref="Encoding"/> to parse the bytes with, or <c>null</c> to use /// <see cref="Encoding.UTF8"/>.</param> /// <param name="converter">The <see cref="ByteConverter"/> to use for converting multibyte data.</param> /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> public static async Task WriteStringsAsync(this Stream stream, IEnumerable <String> values, StringCoding coding = StringCoding.VariableByteCount, Encoding encoding = null, ByteConverter converter = null, CancellationToken cancellationToken = default(CancellationToken)) { await WriteAsync(stream, values, coding, encoding, converter, cancellationToken); }
/// <summary> /// Writes an enumerable of <see cref="String"/> values to the <paramref name="stream"/>. /// </summary> /// <param name="stream">The extended <see cref="Stream"/> instance.</param> /// <param name="values">The values to write.</param> /// <param name="coding">The <see cref="StringCoding"/> determining how the length of the strings is /// stored.</param> /// <param name="encoding">The <see cref="Encoding"/> to parse the bytes with, or <c>null</c> to use /// <see cref="Encoding.UTF8"/>.</param> /// <param name="converter">The <see cref="ByteConverter"/> to use for converting multibyte data.</param> public static void WriteStrings(this Stream stream, IEnumerable <String> values, StringCoding coding = StringCoding.VariableByteCount, Encoding encoding = null, ByteConverter converter = null) { Write(stream, values, coding, encoding, converter); }
/// <summary> /// Writes a <see cref="String"/> value to the <paramref name="stream"/>. /// </summary> /// <param name="stream">The extended <see cref="Stream"/> instance.</param> /// <param name="value">The value to write.</param> /// <param name="coding">The <see cref="StringCoding"/> determining how the length of the string is /// stored.</param> /// <param name="encoding">The <see cref="Encoding"/> to parse the bytes with, or <c>null</c> to use /// <see cref="Encoding.UTF8"/>.</param> /// <param name="converter">The <see cref="ByteConverter"/> to use for converting multibyte data.</param> public static void WriteString(this Stream stream, String value, StringCoding coding = StringCoding.VariableByteCount, Encoding encoding = null, ByteConverter converter = null) { Write(stream, value, coding, encoding, converter); }
public void ReadWriteString() { String[] values = new String[] { String.Empty, "Hallo, Kitty!", "abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "0123456789?!;:'[]{}-_*&^%$#@", "🐔🐓🥚🐣🐤🐥🐻🦆" }; StringCoding[] encodings = new StringCoding[] { StringCoding.ByteCharCount, StringCoding.Int16CharCount, StringCoding.Int32CharCount, StringCoding.VariableByteCount, StringCoding.ZeroTerminated }; ByteConverter[] endianness = new ByteConverter[] { ByteConverter.Big, ByteConverter.Little, ByteConverter.System }; using (MemoryStream stream = new MemoryStream()) { // Prepare test data. foreach (String value in values) { stream.WriteString(value); } foreach (StringCoding encoding in encodings) { foreach (String value in values) { stream.WriteString(value, encoding); } } foreach (ByteConverter endian in endianness) { foreach (String value in values) { stream.WriteString(value, converter: endian); } } foreach (ByteConverter endian in endianness) { foreach (StringCoding encoding in encodings) { foreach (String value in values) { stream.WriteString(value, encoding, converter: endian); } } } // Read test data. stream.Position = 0; foreach (String value in values) { Assert.AreEqual(value, stream.ReadString()); } foreach (StringCoding encoding in encodings) { foreach (String value in values) { Assert.AreEqual(value, stream.ReadString(encoding)); } } foreach (ByteConverter endian in endianness) { foreach (String value in values) { Assert.AreEqual(value, stream.ReadString(converter: endian)); } } foreach (ByteConverter endian in endianness) { foreach (StringCoding encoding in encodings) { foreach (String value in values) { Assert.AreEqual(value, stream.ReadString(encoding, converter: endian)); } } } // Read test data all at once. stream.Position = 0; CollectionAssert.AreEqual(values, stream.ReadStrings(values.Length)); foreach (StringCoding encoding in encodings) { CollectionAssert.AreEqual(values, stream.ReadStrings(values.Length, encoding)); } foreach (ByteConverter endian in endianness) { CollectionAssert.AreEqual(values, stream.ReadStrings(values.Length, converter: endian)); } foreach (ByteConverter endian in endianness) { foreach (StringCoding encoding in encodings) { CollectionAssert.AreEqual(values, stream.ReadStrings(values.Length, encoding, converter: endian)); } } } }
/// <summary> /// Writes a <see cref="String"/> value asynchronously to the <paramref name="stream"/>. /// </summary> /// <param name="stream">The extended <see cref="Stream"/> instance.</param> /// <param name="value">The value to write.</param> /// <param name="coding">The <see cref="StringCoding"/> determining how the length of the string is /// stored.</param> /// <param name="encoding">The <see cref="Encoding"/> to parse the bytes with, or <c>null</c> to use /// <see cref="Encoding.UTF8"/>.</param> /// <param name="converter">The <see cref="ByteConverter"/> to use for converting multibyte data.</param> /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> public static async Task WriteAsync(this Stream stream, String value, StringCoding coding = StringCoding.VariableByteCount, Encoding encoding = null, ByteConverter converter = null, CancellationToken cancellationToken = default(CancellationToken)) { encoding = encoding ?? Encoding.UTF8; converter = converter ?? ByteConverter.System; byte[] textBuffer = encoding.GetBytes(value); switch (coding) { case StringCoding.VariableByteCount: await Write7BitInt32Async(stream, textBuffer.Length, cancellationToken); await stream.WriteAsync(textBuffer, 0, textBuffer.Length, cancellationToken); break; case StringCoding.ByteCharCount: await stream.WriteByteAsync((byte)value.Length, cancellationToken); await stream.WriteAsync(textBuffer, 0, textBuffer.Length, cancellationToken); break; case StringCoding.Int16CharCount: converter.GetBytes((Int16)value.Length, Buffer, 0); await stream.WriteAsync(Buffer, 0, sizeof(Int16), cancellationToken); await stream.WriteAsync(textBuffer, 0, textBuffer.Length, cancellationToken); break; case StringCoding.Int32CharCount: converter.GetBytes(value.Length, Buffer, 0); await stream.WriteAsync(Buffer, 0, sizeof(Int32), cancellationToken); await stream.WriteAsync(textBuffer, 0, textBuffer.Length, cancellationToken); break; case StringCoding.ZeroTerminated: await stream.WriteAsync(textBuffer, 0, textBuffer.Length, cancellationToken); switch (encoding.GetByteCount("A")) { case sizeof(Byte): await stream.WriteByteAsync(0, cancellationToken); break; case sizeof(Int16): await stream.WriteByteAsync(0, cancellationToken); await stream.WriteByteAsync(0, cancellationToken); break; } break; case StringCoding.Raw: await stream.WriteAsync(textBuffer, 0, textBuffer.Length, cancellationToken); break; default: throw new ArgumentException($"Invalid {nameof(StringCoding)}.", nameof(coding)); } }
private void buttonSHA1_Click(object sender, EventArgs e) { richTextBoxOut.Text = StringCoding.EncodeSHA1(richTextBoxIn.Text, ConvertUtil.ConvertEncodingString(comboBoxSHA1.Text)); }
private void buttonMD5_Click(object sender, EventArgs e) { richTextBoxOut.Text = StringCoding.EncodeMD5(richTextBoxIn.Text); }
private void buttonBase64Encode_Click(object sender, EventArgs e) { richTextBoxOut.Text = StringCoding.EncodeBase64(ConvertUtil.ConvertEncodingString(comboBoxBase64.Text), richTextBoxIn.Text); }