public static InfoHash FromBase32(string infoHash)
{
Check.InfoHash(infoHash);
if (infoHash.Length != 32)
{
throw new ArgumentException("Infohash must be a base32 encoded 32 character string");
}
infoHash = infoHash.ToLower();
var infohashOffset = 0;
var hash = new byte[20];
var temp = new byte[8];
for (var i = 0; i < hash.Length;)
{
for (var j = 0; j < 8; j++)
{
if (!Base32DecodeTable.TryGetValue(infoHash[infohashOffset++], out temp[j]))
{
throw new ArgumentException("infoHash", "Value is not a valid base32 encoded string");
}
}
//8 * 5bits = 40 bits = 5 bytes
hash[i++] = (byte)((temp[0] << 3) | (temp[1] >> 2));
hash[i++] = (byte)((temp[1] << 6) | (temp[2] << 1) | (temp[3] >> 4));
hash[i++] = (byte)((temp[3] << 4) | (temp[4] >> 1));
hash[i++] = (byte)((temp[4] << 7) | (temp[5] << 2) | (temp[6] >> 3));
hash[i++] = (byte)((temp[6] << 5) | temp[7]);
}
return(new InfoHash(hash));
}