summary refs log tree commit diff
path: root/crypto/src/bcpg/UserIdPacket.cs
blob: 7d3d3a846f37a0e0a353e6fa76471cd15b2d2254 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using System;
using System.Text;

using Org.BouncyCastle.Utilities;

namespace Org.BouncyCastle.Bcpg
{
    /**
    * Basic type for a user ID packet.
    */
    public class UserIdPacket
        : ContainedPacket, IUserDataPacket
    {
        private readonly byte[] idData;

        public UserIdPacket(BcpgInputStream bcpgIn)
        {
            this.idData = bcpgIn.ReadAll();
        }

		public UserIdPacket(string id)
        {
            this.idData = Encoding.UTF8.GetBytes(id);
        }

        public UserIdPacket(byte[] rawId)
        {
            this.idData = Arrays.Clone(rawId);
        }

        public string GetId()
        {
			return Encoding.UTF8.GetString(idData, 0, idData.Length);
        }

        public byte[] GetRawId() => Arrays.Clone(idData);

        public override bool Equals(object obj)
        {
            if (!(obj is UserIdPacket other))
                return false;

            return Arrays.AreEqual(this.idData, other.idData);
        }

        public override int GetHashCode()
        {
            return Arrays.GetHashCode(this.idData);
        }

        public override void Encode(BcpgOutputStream bcpgOut)
        {
            bcpgOut.WritePacket(PacketTag.UserId, idData);
        }
    }
}