-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathSystem.Security.Cryptography.HMACSHA1.debug.js
More file actions
88 lines (86 loc) · 3.49 KB
/
System.Security.Cryptography.HMACSHA1.debug.js
File metadata and controls
88 lines (86 loc) · 3.49 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
//=============================================================================
// Jocys.com JavaScript.NET Classes (In C# Object Oriented Style)
// Created by Evaldas Jocys <evaldas@jocys.com>
//=============================================================================
/// <reference path="System.debug.js" />
//=============================================================================
// Namespaces
//-----------------------------------------------------------------------------
// <PropertyGroup>
// <RootNamespace>System.Security.Cryptography</RootNamespace>
// <PropertyGroup>
//-----------------------------------------------------------------------------
System.Type.RegisterNamespace("System.Security.Cryptography");
//=============================================================================
System.Security.Cryptography.HMACSHA1 = function (key) {
/// <summary>
/// Represents HMAC-SHA1 hash algorithm class.
/// </summary>
/// <remarks>
/// A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined
/// in FIPS PUB 180-1
/// Version 2.1a Copyright Paul Johnston 2000 - 2002.
/// Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
/// Distributed under the BSD License
/// See http://pajhome.org.uk/crypt/md5 for details.
/// NOTES:
/// Recreated as class by Evaldas Jocys [2006] - SHA1 (160bit) - System.Security.Cryptography.SHA1
/// </remarks>
//---------------------------------------------------------
// Public properties.
this.Name = "HMACSHA1";
this.Algorithm;
this.Key;
this.HashSize = 160;
this.HashName = "SHA1";
//---------------------------------------------------------
this.ComputeHash = function (key, data) {
if (!data) {
data = key;
key = this.Key;
}
// Convert input to byte[] if needed.
if (typeof key === "string") key = System.Text.Encoding.UTF8.GetBytes(key);
if (typeof data === "string") data = System.Text.Encoding.UTF8.GetBytes(data);
return this._ComputeHash(key, data);
};
//---------------------------------------------------------
this.ComputeHashAsHex = function (key, data) {
var bytes = this.ComputeHash(key, data);
return System.BitConverter.ToString(bytes, '');
};
//---------------------------------------------------------
this.ComputeHashAsBase64 = function (key, data) {
var bytes = this.ComputeHash(key, data);
return System.Convert.ToBase64String(bytes, false);
};
//---------------------------------------------------------
this._ComputeHash = function (key, data) {
/// <summary>
/// ComputeHash of a key and some data.
/// </summary>
// if no data then...
if (!data) {
data = key;
key = this.Key;
}
// If key contains more than 64 bytes then use checksum of it as a key.
if (key.length > 64) key = this.Algorithm.ComputeHash(key);
var ipad = new Array(64), opad = new Array(64);
for (var i = 0; i < 64; i++) {
ipad[i] = key[i] ^ 0x36;
opad[i] = key[i] ^ 0x5C;
}
var hash = this.Algorithm.ComputeHash(ipad.concat(data));
return this.Algorithm.ComputeHash(opad.concat(hash));
};
//---------------------------------------------------------
this.Initialize = function () {
this.Algorithm = new System.Security.Cryptography.SHA1();
this.Key = arguments[0];
};
this.Initialize.apply(this, arguments);
};
//==============================================================================
// END
//------------------------------------------------------------------------------