Exemplo n.º 1
0
        /// <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}'");
        }
Exemplo n.º 2
0
        /// <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}'");
        }