kmaragon / Konscious.Security.Cryptography

MIT License
202 stars 20 forks source link

Getting System.NullReferenceException: Object reference not set to an instance of an object #20

Closed westsiderz closed 6 years ago

westsiderz commented 6 years ago

Hello,

I have existing .Net 4.5.2 Framework Class Library project. I included the latest source files (for both Argon2 and Blake2). The build was successful. But when I try to use the GetBytes() function it throws
System.NullReferenceException: Object reference not set to an instance of an object Here is the stack trace: Unhandled Exception: System.AggregateException: One or more errors occurred. ---> System.NullReferenceException: Object reference not set to an instance of an object. at System.Security.Cryptography.HMAC.get_Key() at Konscious.Security.Cryptography.HMACBlake2B.get_Key() in F:\MyRepository\Web\NewPilexLibraries\EDCrypt\SourceCode\Blake2\HMACBlake2B.cs:line 108 at Konscious.Security.Cryptography.HMACBlake2B.Initialize() in F:\MyRepository\Web\NewPilexLibraries\EDCrypt\SourceCode\Blake2\HMACBlake2B.cs:line 123 at Konscious.Security.Cryptography.Argon2Core.Initialize(Byte[] password) in F:\MyRepository\Web\NewPilexLibraries\EDCrypt\SourceCode\Argon2\Argon2Core.cs:line 241 at Konscious.Security.Cryptography.Argon2Core.<InitializeLanes>d__32.MoveNext() in F:\MyRepository\Web\NewPilexLibraries\EDCrypt\SourceCode\Argon2\Argon2Core.cs:line 173 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult() at Konscious.Security.Cryptography.Argon2Core.d__27.MoveNext() in F:\MyRepository\Web\NewPilexLibraries\EDCrypt\SourceCode\Argon2\Argon2Core.cs:line 58 --- End of inner exception stack trace --- at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) at System.Threading.Tasks.Task1.GetResultCore(Boolean waitCompletionNotification) at System.Threading.Tasks.Task1.get_Result() at Konscious.Security.Cryptography.Argon2.GetBytes(Int32 bc) in F:\MyRepository\Web\NewPilexLibraries\EDCrypt\SourceCode\Argon2\Argon2.cs:line 57 at XSecurity.Hasher.GenerateHash(String password, String secret, String uniqueData, Byte[] saltBytes) in F:\MyRepository\Web\NewPilexLibraries\EDCrypt\Hasher.cs:line 100 at XSecurity.Hasher.CreateArgon2Hash(HasherData data) in F:\MyRepository\Web\NewPilexLibraries\EDCrypt\Hasher.cs:line 132 at ConsoleApp1.Program.checkManyHashes() in F:\MyRepository\Web\RePilex\ConsoleApp1\Program.cs:line 63 at ConsoleApp1.Program.Main() in F:\MyRepository\Web\RePilex\ConsoleApp1\Program.cs:line 17`

Is there any specific when building the library. I am using Visual Studio 2017.

At first, I was using the Nuget package and it was working fine. However, it was causing deadlock or something in IIS. So I decided to get the source files and follow the instruction from here https://github.com/kmaragon/Konscious.Security.Cryptography/pull/10. What should I do in order to have this library running on IIS?

Best Regards, Ahmed

kmaragon commented 6 years ago

I think the big thing is to make sure you are setting all of the required parameters. It sounds like perhaps Key want set. That exception you are getting is from the .NET framework in the base class (HMAC) that this inherits from. (Either that or this itself is null)

In IIS, in newer versions of ASP.NET, you'll want to make your controller method async and await GetBytesAsync(). Or you can use GetBytesAsync with the ConfigureAwait(false) method called before calling Wait or result.

kmaragon commented 6 years ago

I will make the ConfigureAwait the default in the next release, when I have time (which will support the hybrid variant), just because this library seems to expose people to this issue pretty frequently. Though it's always a problem when manually waiting on Tasks in IIS (which GetBytes opaquely does).

westsiderz commented 6 years ago

I did as you said and made the method async. It works fine. Thanks a lot for the support and for this great library.