mheyman / Isopoh.Cryptography.Argon2

Fully managed .Net Core implementation of Argon2
Other
184 stars 7 forks source link

Retrieving version being used #38

Closed BigShieldsy closed 1 year ago

BigShieldsy commented 3 years ago

Found this package a great asset for my project, however, I am wondering if there was a way for me to get the version number being used during the hashing process?

mheyman commented 1 year ago

Hope you got your answer before this because it has been a while since you asked, but...

There is an extension method DecodeString() on Argon2Config so you should be able to do something like:

var config = new Argon2Config;
config.DecodeString(justHashedValue, out var hash);
var version = config.Version;

Or, you can use a regex - for Argon2, the version is encoded in the hash string. Look for $v=(\d*), the digits in the capture group are the Argon2 version. As a matter of fact, I'm going to wing a regex to pull out all the relevant parts:

^\$argon2(?<type>i?d?)\$v=(?<version>\d+)\$m=(?<memory>\d+),t=(?<time>\d+),p=(?<parallelism>\d+)(?:,data=(?<data>[A-Za-z0-9/+]+=*))?\$(?<salt>[A-Za-z0-9/+]+=*)\$(?<hash>[A-Za-z0-9/+]+=*)$

This should give you:

  1. hash type: "i", "d", or "id"
  2. version: "16" or "19"
  3. memory cost
  4. time cost
  5. parallelism
  6. optional base64-encoded associated data
  7. base64-encoded salt
  8. base64-encoded hash

Caveat: I haven't tested the C# or regex...