User Tools

Site Tools


sample_code:net_misc

.NET Code Samples

How to encrypt your plain text Token

C#

/// <summary>

/// Creates an HMAC/SHA1 hash.

/// </summary>

/// <param name="clearText">Password + | + CurrentTime</param>

/// <returns>Hex string of the hash value.</returns>

private string CreateHMACSHA1Hash(string clearText)

{

HMACSHA1 hmacSHA1 = new
HMACSHA1(Encoding.ASCII.GetBytes(SubscriptionBridgeAPIKey));

hmacSHA1.ComputeHash(Encoding.ASCII.GetBytes(clearText));

StringBuilder shex = new StringBuilder();

hmacSHA1.Hash.ToList().ForEach(n =>
shex.Append(n.ToString("X02")));

return shex.ToString();

}

VB

'// TOKEN
Dim dt As DateTime = DateTime.Now.ToLocalTime() '// Or use time server to
get correct time
' -> Password|Timestamp
to_hash = password & "|" & dt.ToString("yyyy-MM-ddTHH:mm:00",
CultureInfo.CreateSpecificCulture("en-US"))
password_digest = hex_hmac_sha1(key, to_hash)

Public Function hex_hmac_sha1(ByVal key As String, ByVal to_hash As
String) As String

Dim hmac As New System.Security.Cryptography.HMACSHA1
hmac.Key = System.Text.Encoding.ASCII.GetBytes(key)

hmac.ComputeHash(System.Text.Encoding.ASCII.GetBytes(DirectCast(to_hash,
String)))

Dim _h As String = ""
For Each num As Byte In hmac.Hash
_h += num.ToString("X02")
Next

Return _h

End Function
sample_code/net_misc.txt · Last modified: 2010/11/22 02:44 by matt