チェンジセット 1689 (default)
- 日時:
- 2024/09/13 2:33:19 (3週前)
- 場所:
- framework/trunk
- ファイル:
-
- 3個の更新
凡例:
- 未変更
- 追加
- 削除
-
framework/trunk/CoreLibrary/Sources/Security/SecurityUtility.cs
r1520 r1689 37 37 using System.Web.Security; 38 38 using FCSoft.SilverFrost.Framework.Properties; 39 using FCSoft.SilverFrost.Framework.Text; 39 40 40 41 … … 102 103 103 104 // base64 url encoding に準拠 104 return Convert.ToBase64String(hashedData).Replace('+', '-').Replace('/', '_').TrimEnd('=');105 return TextUtility.EncodeBase64Url(hashedData, true); 105 106 } 106 107 -
framework/trunk/CoreLibrary/Sources/Text/TextUtility.cs
r1688 r1689 843 843 844 844 return buffer; 845 } 846 847 /// <summary> 848 /// <c>base64url</c> エンコーディングを行います。 849 /// </summary> 850 /// <param name="inArray">エンコードするデータ。</param> 851 /// <param name="trimPadding"> 852 /// 末尾のパディング文字を削除する場合は <see langword="true"/>。 853 /// それ以外の場合は <see langword="false"/>。 854 /// </param> 855 /// <returns> 856 /// <c>base64url</c> エンコードした文字列。 857 /// </returns> 858 /// <exception cref="ArgumentNullException"> 859 /// <paramref name="inArray"/> が <see langword="null"/> 参照です。 860 /// </exception> 861 public static string EncodeBase64Url(byte[] inArray, bool trimPadding) 862 { 863 return EncodeBase64Url( 864 inArray, 865 0, 866 // ReSharper disable once ExceptionNotDocumented 867 inArray != null ? inArray.Length : 0, 868 trimPadding); 869 } 870 871 /// <summary> 872 /// <c>base64url</c> エンコーディングを行います。 873 /// </summary> 874 /// <param name="inArray">エンコードするデータ。</param> 875 /// <param name="offset"><paramref name="inArray"/> のオフセット。</param> 876 /// <param name="length">変換する <paramref name="inArray"/> の要素数。</param> 877 /// <param name="trimPadding"> 878 /// 末尾のパディング文字を削除する場合は <see langword="true"/>。 879 /// それ以外の場合は <see langword="false"/>。 880 /// </param> 881 /// <returns> 882 /// <c>base64url</c> エンコードした文字列。 883 /// </returns> 884 /// <exception cref="ArgumentNullException"> 885 /// <paramref name="inArray"/> が <see langword="null"/> 参照です。 886 /// </exception> 887 public static string EncodeBase64Url(byte[] inArray, int offset, int length, bool trimPadding) 888 { 889 string base64 = Convert.ToBase64String(inArray, offset, length).Replace('+', '-').Replace('/', '_'); 890 return trimPadding ? base64.TrimEnd('=') : base64; 891 } 892 893 /// <summary> 894 /// <c>base64url</c> デコードを行います。 895 /// </summary> 896 /// <param name="value"><c>base64url</c> エンコードした文字列。</param> 897 /// <returns> 898 /// <c>base64url</c> デコードしたデータ。 899 /// </returns> 900 /// <exception cref="ArgumentNullException"> 901 /// <paramref name="value"/> が <see langword="null"/> 参照です。 902 /// </exception> 903 public static byte[] DecodeBase64Url(string value) 904 { 905 // 引数をチェック 906 if (value == null) 907 { 908 throw new ArgumentNullException("value"); 909 } 910 911 string base64 = value.Replace('-', '+').Replace('_', '/'); 912 int paddingLength = base64.Length % 4; 913 if (paddingLength > 0) 914 { 915 base64 = base64.PadRight(base64.Length + (4 - paddingLength), '='); 916 } 917 918 return Convert.FromBase64String(base64); 845 919 } 846 920 -
framework/trunk/CoreTest/Sources/Text/TextUtilityTest.cs
r1688 r1689 358 358 359 359 /// <summary> 360 /// <see cref="TextUtility.EncodeBase64Url(byte[], bool)"/> をテストします。 361 /// </summary> 362 /// <param name="inArray">エンコードするデータを表す16進数の文字列。</param> 363 /// <param name="trimPadding"> 364 /// 末尾のパディング文字を削除する場合は <see langword="true"/>。 365 /// それ以外の場合は <see langword="false"/>。 366 /// </param> 367 /// <returns> 368 /// <see cref="TextUtility.EncodeBase64Url(byte[], bool)"/> の戻り値。 369 /// </returns> 370 [Test] 371 [TestCase("", false, ExpectedResult = "")] 372 [TestCase("00", false, ExpectedResult = "AA==")] 373 [TestCase("00", true, ExpectedResult = "AA")] 374 [TestCase("0123456789ABCDEF", false, ExpectedResult = "ASNFZ4mrze8=")] 375 [TestCase("0123456789ABCDEF", true, ExpectedResult = "ASNFZ4mrze8")] 376 [TestCase("00108310518720928B30D38F41149351559761969B71D79F8218A39259A7A29AABB2DBAFC31CB3D35DB7E39EBBF3DFBF", false, ExpectedResult = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_")] 377 public string TestEncodeBase64Url(string inArray, bool trimPadding) 378 { 379 return TextUtility.EncodeBase64Url( 380 TextUtility.ConvertHexToBinary(inArray), 381 trimPadding); 382 } 383 384 /// <summary> 385 /// <see cref="TextUtility.EncodeBase64Url(byte[], int, int, bool)"/> をテストします。 386 /// </summary> 387 /// <param name="inArray">エンコードするデータを表す16進数の文字列。</param> 388 /// <param name="offset"><paramref name="inArray"/> のオフセット。</param> 389 /// <param name="length">変換する <paramref name="inArray"/> の要素数。</param> 390 /// <param name="trimPadding"> 391 /// 末尾のパディング文字を削除する場合は <see langword="true"/>。 392 /// それ以外の場合は <see langword="false"/>。 393 /// </param> 394 /// <returns> 395 /// <see cref="TextUtility.EncodeBase64Url(byte[], int, int, bool)"/> の戻り値。 396 /// </returns> 397 [Test] 398 [TestCase("", 0, 0, false, ExpectedResult = "")] 399 [TestCase("00", 0, 1, false, ExpectedResult = "AA==")] 400 [TestCase("00", 0, 1, true, ExpectedResult = "AA")] 401 [TestCase("0123456789ABCDEF", 0, 8, false, ExpectedResult = "ASNFZ4mrze8=")] 402 [TestCase("0123456789ABCDEF", 0, 8, true, ExpectedResult = "ASNFZ4mrze8")] 403 [TestCase("0123456789ABCDEF", 0, 0, true, ExpectedResult = "")] 404 [TestCase("0123456789ABCDEF", 0, 1, true, ExpectedResult = "AQ")] 405 [TestCase("0123456789ABCDEF", 1, 6, true, ExpectedResult = "I0VniavN")] 406 [TestCase("0123456789ABCDEF", 7, 1, true, ExpectedResult = "7w")] 407 [TestCase("0123456789ABCDEF", 8, 0, true, ExpectedResult = "")] 408 public string TestEncodeBase64Url(string inArray, int offset, int length, bool trimPadding) 409 { 410 return TextUtility.EncodeBase64Url( 411 TextUtility.ConvertHexToBinary(inArray), 412 offset, 413 length, 414 trimPadding); 415 } 416 417 /// <summary> 418 /// <see cref="TextUtility.DecodeBase64Url"/> をテストします。 419 /// </summary> 420 /// <param name="value"><c>base64url</c> エンコードした文字列。</param> 421 /// <returns> 422 /// <see cref="TextUtility.DecodeBase64Url"/> の戻り値を表す16進数の文字列。 423 /// </returns> 424 [Test] 425 [TestCase("", ExpectedResult = "")] 426 [TestCase("AA==", ExpectedResult = "00")] 427 [TestCase("AA", ExpectedResult = "00")] 428 [TestCase("ASNFZ4mrze8=", ExpectedResult = "0123456789ABCDEF")] 429 [TestCase("ASNFZ4mrze8", ExpectedResult = "0123456789ABCDEF")] 430 [TestCase(@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_", ExpectedResult = "00108310518720928B30D38F41149351559761969B71D79F8218A39259A7A29AABB2DBAFC31CB3D35DB7E39EBBF3DFBF")] 431 public string TestDecodeBase64Url(string value) 432 { 433 return TextUtility.ConvertToHexString( 434 TextUtility.DecodeBase64Url(value)); 435 } 436 437 /// <summary> 360 438 /// <see cref="TextUtility.ApplyFixedWidthWordWrap"/> をテストします。 361 439 /// </summary>
※ 詳しい使い方は
TracChangeset を参照してください。