/// <summary> /// Read the key from the text stream. /// </summary> /// <param name="text"> /// A text reader. /// </param> public void Import(TextReader text) { if (text.ReadLine() != CodecName) { throw new FormatException($"Expected '{CodecName}'."); } switch (text.ReadLine()) { case "/base16/": Value = text.ReadLine().ToHexBuffer(); break; case "/base64/": Value = Base64NoPad.FromBase64NoPad(text.ReadLine()); break; default: throw new FormatException("Unknown base encoding."); } }
/// <summary> /// Write the key to text stream. /// </summary> /// <param name="text"> /// A text writer. /// </param> /// <param name="format"> /// Determines how the key <see cref="Value"/> is formatted. Can be /// "base16" or "base64". Defaults to "base16". /// </param> /// <remarks> /// The key is writen as three lines /// (1) the codec name "/key/swarm/psk/1.0.0/" /// (2) the base encoding "/base16/" or "/base64/", /// (3) the key value in the base encoding /// </remarks> public void Export(TextWriter text, string format = "base16") { text.WriteLine(CodecName); text.Write("/"); text.Write(format); text.WriteLine("/"); switch (format) { case "base16": text.WriteLine(Value.ToHexString()); break; case "base64": text.WriteLine(Base64NoPad.ToBase64NoPad(Value)); break; default: throw new Exception($"Unknown encoding '{format}'."); } }