-
Notifications
You must be signed in to change notification settings - Fork 0
/
Encryption.cs
63 lines (54 loc) · 1.67 KB
/
Encryption.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
using System;
using System.Text;
using System.Linq;
using OAwesomeAuth.Extensions;
using System.Collections.Generic;
using System.Security.Cryptography;
namespace OAwesomeAuth
{
public class Encryption
{
public static String SignRequest(String hashLine, OAuthProperties p)
{
String feeder = String.Format ("{0}&{1}", p.ConsumerSecret, p.TokenSecret);
String final = String.Empty;
byte[] feederBytes = Encoding.UTF8.GetBytes (feeder);
switch(p.SignatureMethod){
case(SignatureMethod.HmacSha1):
HMACSHA1 hashEncoder = new HMACSHA1 (feederBytes);
byte[] lineHashBytes = Encoding.UTF8.GetBytes (hashLine);
hashEncoder.ComputeHash (lineHashBytes);
final = Convert.ToBase64String (hashEncoder.Hash);
break;
}
return final.EncodeRfc();
}
public static String GenerateNonce()
{
List<byte> allowedChars = new List<byte> (62);
foreach (byte n in Enumerable.Range (48, 10)) {
allowedChars.Add (n);
}
foreach (byte n in Enumerable.Range (65, 26)) {
allowedChars.Add (n);
}
foreach (byte n in Enumerable.Range (97, 26)) {
allowedChars.Add (n);
}
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider ();
List<Byte> nonceList = new List<Byte> (32);
int i = 1;
do {
byte[] b = new byte[1];
rng.GetBytes (b);
byte bt = b[0];
if (allowedChars.Contains (bt)) {
nonceList.Add (bt);
i++;
}
} while (i <= 32);
UTF8Encoding encoder = new UTF8Encoding ();
return encoder.GetString (nonceList.ToArray ());
}
}
}