Statistics
| Branch: | Revision:

root / trunk / hammock / src / net35 / Hammock / Extensions / OAuthExtensions.cs @ 0eea575a

History | View | Annotate | Download (1.2 kB)

1
using System;
2
using System.Security.Cryptography;
3
using System.Text;
4
using Hammock.Authentication.OAuth;
5

    
6
namespace Hammock.Extensions
7
{
8
    internal static class OAuthExtensions
9
    {
10
        public static string ToRequestValue(this OAuthSignatureMethod signatureMethod)
11
        {
12
            var value = signatureMethod.ToString().ToUpper();
13
            var shaIndex = value.IndexOf("SHA1");
14
            return shaIndex > -1 ? value.Insert(shaIndex, "-") : value;
15
        }
16
        
17
        public static OAuthSignatureMethod FromRequestValue(this string signatureMethod)
18
        {
19
            switch(signatureMethod)
20
            {
21
                case "HMAC-SHA1":
22
                    return OAuthSignatureMethod.HmacSha1;
23
                case "RSA-SHA1":
24
                    return OAuthSignatureMethod.RsaSha1;
25
                default:
26
                    return OAuthSignatureMethod.PlainText;
27
            }
28
        }
29

    
30
        public static string HashWith(this string input, HashAlgorithm algorithm)
31
        {
32
            var data = Encoding.UTF8.GetBytes(input);
33
            var hash = algorithm.ComputeHash(data);
34
            return Convert.ToBase64String(hash);
35
        }
36
    }
37
}