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:
Vyacheslav Napadovsky 2020-03-17 07:15:03 +03:00 committed by GitHub
parent 2a1cc04a32
commit 01cc669960
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 1 deletions

View File

@ -89,7 +89,7 @@ namespace Microsoft.AspNetCore.Identity
uint b1, b2, b3, b4, b5;
int retVal;
switch (offset - input.Length)
switch (input.Length - offset)
{
case 1: retVal = 2; break;
case 2: retVal = 4; break;

View File

@ -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;
}
}
}