-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathSystem.Security.Cryptography.HMACSHA256.debug.js
More file actions
82 lines (80 loc) · 3.13 KB
/
System.Security.Cryptography.HMACSHA256.debug.js
File metadata and controls
82 lines (80 loc) · 3.13 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
//=============================================================================
// 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.HMACSHA256 = function (key) {
/// <summary>
/// Represents HMAC-SHA256 hash algorithm class.
/// </summary>
/// <remarks>
/// NOTES:
/// Recreated as class by Evaldas Jocys [2017]
/// </remarks>
//---------------------------------------------------------
// Public properties.
this.Name = "HMACSHA256";
this.Algorithm;
this.Key;
this.HashSize = 256;
this.HashName = "SHA256";
//---------------------------------------------------------
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.SHA256();
this.Key = arguments[0];
};
this.Initialize.apply(this, arguments);
};
//==============================================================================
// END
//------------------------------------------------------------------------------