Repository navigation
Expand file tree
/
Copy pathUtils.fs
More file actions
201 lines (172 loc) · 4.37 KB
/
Copy pathUtils.fs
File metadata and controls
201 lines (172 loc) · 4.37 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
module Srp.Utils
open System.Security.Cryptography
open System.Numerics
open System.Globalization
open Extensions
//Hash Algorithm type
type HashAlg =
| SHA1
| SHA256
/// **Description**
/// * Reverses the order of the array, converts to
/// * Big integer, and if negative, applys bit shift
/// operation
/// **Parameters**
/// * `bytes` - parameter of type `byte []`
///
/// **Output Type**
/// * `BigInteger`
///
/// **Exceptions**
///
let bytesToBigInt (bytes : byte[]) =
Array.rev bytes
|> BigInteger
|> function
| bigInt when bigInt < BigInteger.Zero -> (BigInteger.One <<< (bytes.Length * 8)) + bigInt;
| bigInt -> bigInt
/// **Description**
/// * Converts big integer to bytes, reverses order
/// * And trims beggining if it is 0
/// **Parameters**
/// * `bigInt` - parameter of type `BigInteger`
///
/// **Output Type**
/// * `byte []`
///
/// **Exceptions**
///
let bigIntToBytes (bigInt : BigInteger) =
bigInt
|> BigInteger.getBytes
|> Array.rev
|> Array.removeFirstIf 0uy
/// **Description**
/// * Removes whitespace from hex string and
/// * Tries to parse to big integer.
/// * If unsuccessful it returns a zero big integer
/// **Parameters**
/// * `hex` - parameter of type `string`
///
/// **Output Type**
/// * `Result<BigInteger,string>`
///
/// **Exceptions**
///
let hexToBigInt (hex : string) =
try
("0" + hex
|> Regex.replace @"\s" System.String.Empty
|> BigInteger.parse NumberStyles.HexNumber)
|> Ok
with error ->
error |> Error
/// **Description**
/// * Iterates through all bytes and converts
/// * to hexadecimal character and concatenates
/// * to string
/// **Parameters**
/// * `bytes` - parameter of type `byte []`
///
/// **Output Type**
/// * `string`
///
/// **Exceptions**
///
let bytesToHex bytes =
bytes
|> Array.map (fun (b : byte) -> sprintf "%x" b)
|> String.concat System.String.Empty
/// **Description**
/// * Gets bytes from big integer and converts it to hexadecimal string
/// **Parameters**
/// * `value` - parameter of type `bigint`
///
/// **Output Type**
/// * `string`
///
/// **Exceptions**
///
let bigIntToHex (value : bigint) =
value
|> BigInteger.getBytes
|> bytesToHex
/// **Description**
/// * Calculates difference between byte array and required length
/// * Appends that amount in a form of a zero array to beggining of
/// byte array
/// **Parameters**
/// * `bytes` - parameter of type `byte []`
/// * `length` - parameter of type `int`
///
/// **Output Type**
/// * `byte []`
///
/// **Exceptions**
///
let padTo (bytes:byte[]) length =
let paddingLen = match (length - bytes.Length) with
| p when p >= 0 -> p
| _ -> 0
Array.append (Array.zeroCreate(paddingLen)) bytes
/// **Description**
/// * Pads byte array to the amount of bytes of srp parameter
/// **Parameters**
/// * `constant` - parameter of type `SRPParameter`
/// * `bytes` - parameter of type `byte []`
///
/// **Output Type**
/// * `byte []`
///
/// **Exceptions**
///
let padToN bits bytes =
padTo bytes (bits/8)
let bigIntPaddedToNBytes bits bigInt =
bigInt
|> bigIntToBytes
|> padToN bits
/// **Description**
/// * Chooses hash algorithm based on __hashAlg__
/// **Parameters**
/// * `hashAlg` - parameter of type `HashAlg`
///
/// **Output Type**
/// * `HashAlgorithm`
///
/// **Exceptions**
///
let hashAlgorithm hashAlg : HashAlgorithm =
match hashAlg with
| SHA1 -> SHA1.Create() :> HashAlgorithm
| SHA256 -> SHA256.Create() :> HashAlgorithm
/// **Description**
/// * Computes hash of byte array with given
/// * hashing algorithm
/// **Parameters**
/// * `hashAlg` - parameter of type `HashAlg`
/// * `bytes` - parameter of type `byte []`
///
/// **Output Type**
/// * `byte []`
///
/// **Exceptions**
///
let bytesToHash hashAlg (bytes:byte[]) =
bytes
|> (hashAlg |> hashAlgorithm).ComputeHash
/// **Description**
/// * Generates random buffer using __RNGCryptoServiceProvider__
/// **Parameters**
/// * `arraySize` - parameter of type `int`
///
/// **Output Type**
/// * `byte []`
///
/// **Exceptions**
///
let generateRandom arraySize =
let generator = new RNGCryptoServiceProvider()
let buffer = Array.zeroCreate arraySize
generator.GetBytes buffer
buffer