public void InfoHashTest() { MagnetLink link = new MagnetLink("magnet:?xt.1=urn:sha1:YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C"); Assert.Equal(InfoHash.FromBase32("YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C"), link.InfoHash); //base32 InfoHash initial = new InfoHash(System.Text.Encoding.ASCII.GetBytes("foobafoobafoobafooba")); link = new MagnetLink("magnet:?xt=urn:sha1:MZXW6YTBMZXW6YTBMZXW6YTBMZXW6YTB"); Assert.Equal(initial, link.InfoHash); //base40 = hex InfoHash hash = Create(); string hex = hash.ToHex(); link = new MagnetLink("magnet:?xt=urn:btih:" + hex); Assert.Equal(hash, link.InfoHash); }
public static InfoHash ToInfoHash(this Magnet magnet) { if (string.IsNullOrEmpty(magnet.BTIH)) { throw new InvalidDataException("Unable to create InfoHash. Expected bit torrent info hash but null or empty"); } if (magnet.BTIH.Length == 40) { return(InfoHash.FromHex(magnet.BTIH)); } if (magnet.BTIH.Length == 32) { return(InfoHash.FromBase32(magnet.BTIH)); } throw new InvalidDataException("Unknown type of hash provided. Expected BTIH of length 32 or 40 chars"); }