예제 #1
0
파일: PgMD5Helper.cs 프로젝트: CDuke/PgNet
        public const int PasswordHashLength = 3 + MD5HashHexSize; // s_md5DeclarationBytes + MD5HashHexSize

        public static void ComputePassword(string user, string password, ReadOnlySpan <byte> salt, Span <byte> result)
        {
            var passwordBytesCount = PgUtf8.GetByteCount(password);
            var userBytesCount     = PgUtf8.GetByteCount(user);

            var         tempSize = passwordBytesCount + userBytesCount + MD5HashSize + MD5HashHexSize + salt.Length;
            Span <byte> temp     = stackalloc byte[tempSize];

            var buffer     = temp.Slice(0, passwordBytesCount + userBytesCount);
            var writeCount = PgUtf8.ToUtf8(password, buffer);

            _ = PgUtf8.ToUtf8(user, buffer.Slice(writeCount));
            var hash = temp.Slice(buffer.Length, MD5HashSize);

            MD5.Instance.TryComputeHash(buffer, hash);

            var hexHash = temp.Slice(buffer.Length + hash.Length);

            HashToString(hash, hexHash.Slice(0, MD5HashHexSize));
            salt.CopyTo(hexHash.Slice(MD5HashHexSize));

            MD5.Instance.TryComputeHash(hexHash, hash);
            hexHash = hexHash.Slice(0, MD5HashHexSize);
            HashToString(hash, hexHash);

            s_md5DeclarationBytes.CopyTo(result);

            hexHash.CopyTo(result.Slice(3));
        }
예제 #2
0
        public void WriteNullTerminateUtf8String(ReadOnlySpan <char> s)
        {
            var slice = m_span.Slice(m_position);
            var count = PgUtf8.ToUtf8(s, slice);

            slice[count] = 0;
            m_position  += count + 1;
        }