public bool isChashValid(string encoded) { var encodedLen = encoded.Length; if (encodedLen != 32 && encodedLen != 48) // 160/5 = 32, 288/6 = 48 { throw new Exception("wrong encoded length: " + encodedLen); } try { var chash = encodedLen == 32 ? Base32.Decode(encoded) : Convert.FromBase64String(encoded); var binChash = buffer2bin(chash); var separated = SeparateIntoCleanDataAndChecksum(binChash); var cleanData = bin2buffer(separated.Item1); var checksum = bin2buffer(separated.Item2); return(checksum.Equals(getChecksum(cleanData))); } catch { return(false); } }
private string GetChash(string data, int chashLength) { CheckLength(chashLength); var dataBytes = Encoding.UTF8.GetBytes(data); var hash = createHash(chashLength == 160 ? "ripemd160" : "sha256", dataBytes); //console.log("hash", hash); var truncatedHash = chashLength == 160 ? hash.Slice(4) : hash; // drop first 4 bytes if 160 //console.log("clean data", truncated_hash); var checksum = getChecksum(truncatedHash); //console.log("checksum", checksum); //console.log("checksum", buffer2bin(checksum)); var binCleanData = buffer2bin(truncatedHash); var binChecksum = buffer2bin(checksum); var binChash = mixChecksumIntoCleanData(binCleanData, binChecksum); //console.log(binCleanData.length, binChecksum.length, binChash.length); var chash = bin2buffer(binChash); //console.log("chash ", chash); var encoded = chashLength == 160 ? Base32.Encode(chash) : Convert.ToBase64String(chash); //console.log(encoded); return(encoded); }