private KnownHost(string hostName, string keyType, string base64Pubkey, HostPubkeyMarker marker) { _isHashed = hostName.StartsWith("|"); if (_isHashed) { if (!hostName.StartsWith(HashedHostSignifier)) { throw new FormatException("The hashed section was not properly composed"); } string[] splitHashedSection = hostName.Split('|'); if (splitHashedSection.Length != 4) { throw new FormatException("The hashed section was not properly composed"); } _hashSalt = Convert.FromBase64String(splitHashedSection[2]); if (_hashSalt.Length != 20) { throw new ArgumentException("The salt must be exacly 20 bytes"); } _hashedHostName = Convert.FromBase64String(splitHashedSection[3]); } else { _plaintextHostPatterns = GetPatternList(hostName); if (_plaintextHostPatterns.Count == 0) { throw new FormatException("No hostname patterns given"); } } _keyType = keyType; _pubKey = Convert.FromBase64String(base64Pubkey); _hostMarker = marker; }
private static bool UnsafeTryParse(string hostLine, out KnownHost host) { host = null; if (string.IsNullOrEmpty(hostLine) || hostLine.StartsWith("#")) { return(false); } bool hasSpecialPrefix = hostLine.StartsWith("@"); string[] sectionedLine = hostLine.Split(' '); if (hasSpecialPrefix && sectionedLine.Length < 4) { return(false); } else if (sectionedLine.Length < 3) { return(false); } HostPubkeyMarker marker = hasSpecialPrefix ? ParseSpecialOperand(sectionedLine[0]) : HostPubkeyMarker.None; if (marker == HostPubkeyMarker.Error) { return(false); } int sectionOffset = hasSpecialPrefix ? 1 : 0; string hostNameSecion = sectionedLine[sectionOffset]; string keyTypeSection = sectionedLine[1 + sectionOffset]; string pubKeySection = sectionedLine[2 + sectionOffset]; host = new KnownHost(hostNameSecion, keyTypeSection, pubKeySection, marker); return(true); }