/// <summary> /// Tries to parse the specified hexadecimal. /// </summary> /// <param name="hex">The hexadecimal.</param> /// <param name="value">The value.</param> /// <returns></returns> public static bool TryParse(ReadOnlySpan <char> hex, out Sha1 value) { value = default; Span <byte> sha = stackalloc byte[ByteLength]; if (!ShaUtil.TryParse(hex, sha)) { return(false); } value = new Sha1(sha); return(true); }
/// <summary> /// Converts the <see cref="Sha1"/> instance to a string using the 'n' or 'N' format, /// and returns the value split into two tokens. /// </summary> /// <param name="prefixLength">The length of the first token.</param> /// <param name="uppercase">If True, output uppercase, else output lowercase.</param> public KeyValuePair <string, string> Split(int prefixLength, bool uppercase = false) { ShaUtil.HexCasing casing = uppercase ? ShaUtil.HexCasing.Upper : ShaUtil.HexCasing.Lower; unsafe { fixed(byte *ptr = _block.Bytes) { var sha = new ReadOnlySpan <byte>(ptr, ByteLength); KeyValuePair <string, string> kvp = ShaUtil.Split(sha, prefixLength, casing); return(kvp); } } }
/// <summary> /// Tries to parse the specified hexadecimal. /// </summary> /// <param name="hex">The hexadecimal.</param> /// <param name="value">The value.</param> /// <returns></returns> public static bool TryParse(string hex, out Sha1 value) { value = default; if (hex is null || hex.Length < HexLength) { return(false); } Span <byte> sha = stackalloc byte[ByteLength]; if (!ShaUtil.TryParse(hex.AsSpan(), sha)) { return(false); } value = new Sha1(sha); return(true); }
/// <summary> /// Returns a string representation of the <see cref="Sha1"/> instance. /// n: a9993e364706816aba3e25717850c26c9cd0d89d, /// d: a9993e36-4706816a-ba3e2571-7850c26c-9cd0d89d, /// s: a9993e36 4706816a ba3e2571 7850c26c 9cd0d89d /// </summary> /// <param name="format"></param> public string ToString(string format) { if (string.IsNullOrWhiteSpace(format)) { throw new FormatException($"Empty format specification"); } if (format.Length != 1) { throw new FormatException($"Invalid format specification length {format.Length}"); } unsafe { fixed(byte *ptr = _block.Bytes) { var sha = new ReadOnlySpan <byte>(ptr, ByteLength); switch (format[0]) { // a9993e364706816aba3e25717850c26c9cd0d89d case 'n': return(ShaUtil.ToString(sha, ShaUtil.HexCasing.Lower)); case 'N': return(ShaUtil.ToString(sha, ShaUtil.HexCasing.Upper)); // a9993e36-4706816a-ba3e2571-7850c26c-9cd0d89d case 'd': return(ShaUtil.ToString(sha, '-', ShaUtil.HexCasing.Lower)); case 'D': return(ShaUtil.ToString(sha, '-', ShaUtil.HexCasing.Upper)); // a9993e36 4706816a ba3e2571 7850c26c 9cd0d89d case 's': return(ShaUtil.ToString(sha, ' ', ShaUtil.HexCasing.Lower)); case 'S': return(ShaUtil.ToString(sha, ' ', ShaUtil.HexCasing.Upper)); } } } throw new FormatException($"Invalid format specification '{format}'"); }
/// <summary> /// Returns a string representation of the <see cref="Sha256"/> instance. /// n: cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0, /// d: cdc76e5c-9914fb92-81a1c7e2-84d73e67-f1809a48-a497200e-046d39cc-c7112cd0, /// s: cdc76e5c 9914fb92 81a1c7e2 84d73e67 f1809a48 a497200e 046d39cc c7112cd0 /// </summary> /// <param name="format"></param> /// <returns></returns> public string ToString(string format) { if (string.IsNullOrWhiteSpace(format)) { throw new FormatException($"Empty format specification"); } if (format.Length != 1) { throw new FormatException($"Invalid format specification length {format.Length}"); } unsafe { fixed(byte *ptr = _block.Bytes) { var sha = new ReadOnlySpan <byte>(ptr, ByteLength); switch (format[0]) { // cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0 case 'n': return(ShaUtil.ToString(sha, ShaUtil.HexCasing.Lower)); case 'N': return(ShaUtil.ToString(sha, ShaUtil.HexCasing.Upper)); // cdc76e5c-9914fb92-81a1c7e2-84d73e67-f1809a48-a497200e-046d39cc-c7112cd0 case 'd': return(ShaUtil.ToString(sha, '-', ShaUtil.HexCasing.Lower)); case 'D': return(ShaUtil.ToString(sha, '-', ShaUtil.HexCasing.Upper)); // cdc76e5c 9914fb92 81a1c7e2 84d73e67 f1809a48 a497200e 046d39cc c7112cd0 case 's': return(ShaUtil.ToString(sha, ' ', ShaUtil.HexCasing.Lower)); case 'S': return(ShaUtil.ToString(sha, ' ', ShaUtil.HexCasing.Upper)); } } } throw new FormatException($"Invalid format specification '{format}'"); }
/// <summary> /// Parses the specified hexadecimal. /// </summary> /// <param name="hex">The hexadecimal.</param> /// <returns></returns> /// <exception cref="FormatException">Sha1</exception> public static Sha1 Parse(string hex) { if (hex is null) { throw new ArgumentNullException(nameof(hex)); } if (hex.Length < HexLength) { throw new FormatException($"Input was not recognized as a valid {nameof(Sha1)}"); } Span <byte> sha = stackalloc byte[ByteLength]; if (!ShaUtil.TryParse(hex.AsSpan(), sha)) { throw new FormatException($"Input was not recognized as a valid {nameof(Sha1)}"); } var value = new Sha1(sha); return(value); }