This repository has been archived by the owner on Dec 30, 2020. It is now read-only.
/
Oid.cs
123 lines (110 loc) · 4.27 KB
/
Oid.cs
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using System;
using System.Text.RegularExpressions;
using MongoDB.Driver.Bson;
namespace MongoDB.Driver{
public class Oid
{
/// <summary>
/// Gets or sets the value.
/// </summary>
/// <value>The value.</value>
public byte[] Value { get; set; }
/// <summary>
/// Gets the created.
/// </summary>
/// <value>The created.</value>
public DateTime Created{
get{
byte[] time = new byte[4];
Array.Copy(this.Value,time,4);
Array.Reverse(time);
int seconds = BitConverter.ToInt32(time,0);
return BsonInfo.Epoch.AddSeconds(seconds);
}
}
/// <summary>
/// Initializes a new instance of the <see cref="Oid"/> class.
/// </summary>
public Oid(){}
/// <summary>
/// Initializes a new instance of the <see cref="Oid"/> class.
/// </summary>
/// <param name="value">The value.</param>
public Oid(string value){
ValidateHex(value);
this.Value = DecodeHex(value);
}
/// <summary>
/// Initializes a new instance of the <see cref="Oid"/> class.
/// </summary>
/// <param name="value">The value.</param>
public Oid(byte[] value){
this.Value = value;
}
/// <summary>
/// Determines whether the specified <see cref="System.Object"/> is equal to this instance.
/// </summary>
/// <param name="obj">The <see cref="System.Object"/> to compare with this instance.</param>
/// <returns>
/// <c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.
/// </returns>
/// <exception cref="T:System.NullReferenceException">
/// The <paramref name="obj"/> parameter is null.
/// </exception>
public override bool Equals(object obj){
if(obj.GetType() == typeof(Oid)){
string hex = obj.ToString();
return this.ToString().Equals(hex);
}
return false;
}
/// <summary>
/// Returns a hash code for this instance.
/// </summary>
/// <returns>
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
/// </returns>
public override int GetHashCode(){
return ToString().GetHashCode();
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents this instance.
/// </summary>
/// <returns>
/// A <see cref="System.String"/> that represents this instance.
/// </returns>
public override string ToString() {
return String.Format("\"{0}\"",BitConverter.ToString(Value).Replace("-","").ToLower());
}
/// <summary>
/// Validates the hex.
/// </summary>
/// <param name="value">The value.</param>
protected void ValidateHex(string value){
if(value == null || value.Length != 24) throw new ArgumentException("Oid strings should be 24 characters");
Regex notHexChars = new Regex(@"[^A-Fa-f0-9]", RegexOptions.None);
if(notHexChars.IsMatch(value)){
throw new ArgumentOutOfRangeException("value","Value contains invalid characters");
}
}
/// <summary>
/// Decodes the hex.
/// </summary>
/// <param name="value">The value.</param>
/// <returns></returns>
protected static byte[] DecodeHex(string value){
int numberChars = value.Length;
byte[] bytes = new byte[numberChars / 2];
for (int i = 0; i < numberChars; i += 2){
try{
bytes[i / 2] = Convert.ToByte(value.Substring(i, 2), 16);
}
catch{
//failed to convert these 2 chars, they may contain illegal charracters
bytes[i / 2] = 0;
}
}
return bytes;
}
}
}