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)); }
public InfoHash(byte[] infoHash) { Check.InfoHash(infoHash); if (infoHash.Length != 20) { throw new ArgumentException("Infohash must be exactly 20 bytes long"); } Hash = (byte[])infoHash.Clone(); }
public static InfoHash FromHex(string infoHash) { Check.InfoHash(infoHash); if (infoHash.Length != 40) { throw new ArgumentException("Infohash must be 40 characters long"); } var hash = new byte[20]; for (var i = 0; i < hash.Length; i++) { hash[i] = byte.Parse(infoHash.Substring(i * 2, 2), NumberStyles.HexNumber); } return(new InfoHash(hash)); }
public static InfoHash UrlDecode(string infoHash) { Check.InfoHash(infoHash); return(new InfoHash(UriHelper.UrlDecode(infoHash))); }