Bugfix for Microsoft.AspNetCore.Identity.Base32.ToBase32 function (#19621)
* Bugfix for Microsoft.AspNetCore.Identity.Base32.ToBase32 function * Add Base32 class tests
This commit is contained in:
parent
2a1cc04a32
commit
01cc669960
|
|
@ -89,7 +89,7 @@ namespace Microsoft.AspNetCore.Identity
|
||||||
uint b1, b2, b3, b4, b5;
|
uint b1, b2, b3, b4, b5;
|
||||||
|
|
||||||
int retVal;
|
int retVal;
|
||||||
switch (offset - input.Length)
|
switch (input.Length - offset)
|
||||||
{
|
{
|
||||||
case 1: retVal = 2; break;
|
case 1: retVal = 2; break;
|
||||||
case 2: retVal = 4; break;
|
case 2: retVal = 4; break;
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,40 @@
|
||||||
|
// Copyright (c) .NET Foundation. All rights reserved.
|
||||||
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Security.Cryptography;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Microsoft.AspNetCore.Identity.Test
|
||||||
|
{
|
||||||
|
public class Base32Test
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void ConversionTest()
|
||||||
|
{
|
||||||
|
var data = new byte[] { 1, 2, 3, 4, 5, 6 };
|
||||||
|
Assert.Equal<byte>(data, Base32.FromBase32(Base32.ToBase32(data)));
|
||||||
|
|
||||||
|
int length;
|
||||||
|
do {
|
||||||
|
length = GetRandomByteArray(1)[0];
|
||||||
|
} while (length % 5 == 0);
|
||||||
|
data = GetRandomByteArray(length);
|
||||||
|
Assert.Equal<byte>(data, Base32.FromBase32(Base32.ToBase32(data)));
|
||||||
|
|
||||||
|
length = (int)(GetRandomByteArray(1)[0]) * 5;
|
||||||
|
data = GetRandomByteArray(length);
|
||||||
|
Assert.Equal<byte>(data, Base32.FromBase32(Base32.ToBase32(data)));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static readonly RandomNumberGenerator _rng = RandomNumberGenerator.Create();
|
||||||
|
|
||||||
|
private static byte[] GetRandomByteArray(int length) {
|
||||||
|
byte[] bytes = new byte[length];
|
||||||
|
_rng.GetBytes(bytes);
|
||||||
|
return bytes;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue