-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathUtils.cs
170 lines (139 loc) · 5.34 KB
/
Utils.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
DSA Lims - Laboratory Information Management System
Copyright (C) 2018 Norwegian Radiation Protection Authority
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Authors: Dag Robole,
using System;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
namespace DSA_lims
{
public static class Utils
{
public static readonly string NumberSeparator = CultureInfo.InvariantCulture.NumberFormat.NumberDecimalSeparator;
public static readonly char NumberSeparatorChar = Convert.ToChar(NumberSeparator);
public static int MIN_USERNAME_LENGTH { get { return 3; } }
public static int MIN_PASSWORD_LENGTH { get { return 8; } }
public static readonly string DateFormatISO = "yyyy-MM-dd";
public static readonly string TimeFormatISO = "HH:mm:ss";
public static readonly string DateTimeFormatISO = DateFormatISO + " " + TimeFormatISO;
public static readonly string DateFormatNorwegian = "dd.MM.yyyy";
public static readonly string TimeFormatNorwegian = "HH:mm:ss";
public static readonly string DateTimeFormatNorwegian = DateFormatNorwegian + " " + TimeFormatNorwegian;
public static readonly string ScientificFormat = "0.###E+0";
public static string makeStatusMessage(string msg)
{
return DateTime.Now.ToString(DateTimeFormatNorwegian) + " - " + msg;
}
public static string makeErrorMessage(string msg)
{
return msg + ". See log for more details";
}
public static bool IsValidGuid(object id)
{
if (id == null)
return false;
Guid g;
if(!Guid.TryParse(id.ToString(), out g) || g == Guid.Empty)
return false;
return true;
}
public static bool IsValidInteger(string s)
{
int n;
return int.TryParse(s, out n);
}
public static bool IsValidDecimal(string s)
{
double d;
return double.TryParse(s, out d);
}
public static Guid MakeGuid(object o)
{
if (o == null || o == DBNull.Value)
return Guid.Empty;
Guid g;
if (!Guid.TryParse(o.ToString(), out g) || g == Guid.Empty)
return Guid.Empty;
return Guid.Parse(o.ToString());
}
public static int? ToInt32(object o)
{
if (o == null)
return null;
if (o.GetType() == typeof(String) && o.ToString().Trim() == "")
return null;
return Convert.ToInt32(o);
}
public static double? ToDouble(object o)
{
if (o == null)
return null;
if (o.GetType() == typeof(String) && o.ToString().Trim() == "")
return null;
return Convert.ToDouble(o);
}
public static string ToString(this double? self, string format)
{
if (self == null)
return "";
return self.Value.ToString(ScientificFormat);
}
public static byte[] MakePasswordHash(string password, string username)
{
return MakePasswordHash(Encoding.UTF8.GetBytes(password), Encoding.UTF8.GetBytes(username.ToUpper().Substring(0, 3)));
}
private static byte[] MakePasswordHash(byte[] value, byte[] salt)
{
byte[] saltedValue = value.Concat(salt).ToArray();
return new SHA256Managed().ComputeHash(saltedValue);
}
public static bool PasswordHashEqual(byte[] hash1, byte[] hash2)
{
return hash1.SequenceEqual(hash2);
}
public static byte[] Compress(string s)
{
var bytes = Encoding.Unicode.GetBytes(s);
using (var msi = new MemoryStream(bytes))
{
using (var mso = new MemoryStream())
{
using (var gs = new GZipStream(mso, CompressionMode.Compress))
{
msi.CopyTo(gs);
}
return mso.ToArray();
}
}
}
public static string Decompress(byte[] b)
{
using (var msi = new MemoryStream(b))
{
using (var mso = new MemoryStream())
{
using (var gs = new GZipStream(msi, CompressionMode.Decompress))
{
gs.CopyTo(mso);
}
return Encoding.Unicode.GetString(mso.ToArray());
}
}
}
}
}