Skip to content

Commit 728a11d

Browse files
chore(npm): Temporarily include lib/ dir in git (for release candidate)
1 parent 319d6ab commit 728a11d

31 files changed

Lines changed: 3066 additions & 1 deletion

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
docs
22
node_modules
3+
34
# Compiled lib directory
4-
lib
5+
# lib
6+
57
# Bundles created by Webpack
68
dist

lib/algorithms/HMAC.js

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
'use strict';
2+
3+
/**
4+
* Dependencies
5+
* @ignore
6+
*/
7+
8+
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
9+
10+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
11+
12+
var base64url = require('base64url');
13+
var crypto = require('@trust/webcrypto');
14+
var TextEncoder = require('../text-encoder');
15+
16+
/**
17+
* HMAC with SHA-2 Functions
18+
*/
19+
20+
var HMAC = function () {
21+
22+
/**
23+
* Constructor
24+
*
25+
* @param {string} bitlength
26+
*/
27+
function HMAC(params) {
28+
_classCallCheck(this, HMAC);
29+
30+
this.params = params;
31+
}
32+
33+
/**
34+
* Sign
35+
*
36+
* @description
37+
* Generate a hash-based message authentication code for a
38+
* given input and key. Enforce the key length is equal to
39+
* or greater than the bitlength.
40+
*
41+
* @param {CryptoKey} key
42+
* @param {string} data
43+
*
44+
* @returns {string}
45+
*/
46+
47+
48+
_createClass(HMAC, [{
49+
key: 'sign',
50+
value: function sign(key, data) {
51+
var algorithm = this.params;
52+
53+
// TODO: validate key length
54+
55+
data = new TextEncoder().encode(data);
56+
57+
return crypto.subtle.sign(algorithm, key, data).then(function (signature) {
58+
return base64url(Buffer.from(signature));
59+
});
60+
}
61+
62+
/**
63+
* Verify
64+
*
65+
* @description
66+
* Verify a digital signature for a given input and private key.
67+
*
68+
* @param {CryptoKey} key
69+
* @param {string} signature
70+
* @param {string} data
71+
*
72+
* @returns {Boolean}
73+
*/
74+
75+
}, {
76+
key: 'verify',
77+
value: function verify(key, signature, data) {
78+
var algorithm = this.params;
79+
80+
if (typeof signature === 'string') {
81+
signature = Uint8Array.from(base64url.toBuffer(signature));
82+
}
83+
84+
if (typeof data === 'string') {
85+
data = new TextEncoder().encode(data);
86+
}
87+
88+
return crypto.subtle.verify(algorithm, key, signature, data);
89+
}
90+
91+
/**
92+
* Assert Sufficient Key Length
93+
*
94+
* @description Assert that the key length is sufficient
95+
* @param {string} key
96+
*/
97+
98+
}, {
99+
key: 'assertSufficientKeyLength',
100+
value: function assertSufficientKeyLength(key) {
101+
if (key.length < this.bitlength) {
102+
throw new Error('The key is too short.');
103+
}
104+
}
105+
}]);
106+
107+
return HMAC;
108+
}();
109+
110+
/**
111+
* Export
112+
*/
113+
114+
115+
module.exports = HMAC;

lib/algorithms/NONE.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict';
2+
3+
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
4+
5+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
6+
7+
/**
8+
* None
9+
*/
10+
var None = function () {
11+
function None() {
12+
_classCallCheck(this, None);
13+
}
14+
15+
_createClass(None, [{
16+
key: 'sign',
17+
18+
/**
19+
* sign
20+
*/
21+
value: function sign() {
22+
return Promise.resolve('');
23+
}
24+
25+
/**
26+
* verify
27+
*/
28+
29+
}, {
30+
key: 'verify',
31+
value: function verify() {
32+
// this will never get called. but you looked.
33+
}
34+
}]);
35+
36+
return None;
37+
}();
38+
39+
/**
40+
* Export
41+
*/
42+
43+
44+
module.exports = None;
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
'use strict';
2+
3+
/**
4+
* Dependencies
5+
* @ignore
6+
*/
7+
8+
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
9+
10+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
11+
12+
var base64url = require('base64url');
13+
var crypto = require('@trust/webcrypto');
14+
var TextEncoder = require('../text-encoder');
15+
16+
/**
17+
* RSASSA-PKCS1-v1_5
18+
*/
19+
20+
var RSASSA_PKCS1_v1_5 = function () {
21+
22+
/**
23+
* constructor
24+
*
25+
* @param {string} bitlength
26+
*/
27+
function RSASSA_PKCS1_v1_5(params) {
28+
_classCallCheck(this, RSASSA_PKCS1_v1_5);
29+
30+
this.params = params;
31+
}
32+
33+
/**
34+
* sign
35+
*
36+
* @description
37+
* Generate a digital signature for a given input and private key.
38+
*
39+
* @param {CryptoKey} key
40+
* @param {BufferSource} data
41+
*
42+
* @returns {Promise}
43+
*/
44+
45+
46+
_createClass(RSASSA_PKCS1_v1_5, [{
47+
key: 'sign',
48+
value: function sign(key, data) {
49+
var algorithm = this.params;
50+
51+
// TODO
52+
//if (!this.sufficientKeySize()) {
53+
// return Promise.reject(
54+
// new Error(
55+
// 'A key size of 2048 bits or larger must be used with RSASSA-PKCS1-v1_5'
56+
// )
57+
// )
58+
//}
59+
60+
data = new TextEncoder().encode(data);
61+
62+
return crypto.subtle.sign(algorithm, key, data).then(function (signature) {
63+
return base64url(Buffer.from(signature));
64+
});
65+
}
66+
67+
/**
68+
* verify
69+
*
70+
* @description
71+
* Verify a digital signature for a given input and private key.
72+
*
73+
* @param {CryptoKey} key
74+
* @param {BufferSource} signature
75+
* @param {BufferSource} data
76+
*
77+
* @returns {Promise}
78+
*/
79+
80+
}, {
81+
key: 'verify',
82+
value: function verify(key, signature, data) {
83+
var algorithm = this.params;
84+
85+
if (typeof signature === 'string') {
86+
signature = Uint8Array.from(base64url.toBuffer(signature));
87+
}
88+
89+
if (typeof data === 'string') {
90+
data = new TextEncoder().encode(data);
91+
}
92+
// ...
93+
94+
return crypto.subtle.verify(algorithm, key, signature, data);
95+
}
96+
97+
/**
98+
* importKey
99+
*
100+
* @param {JWK} key
101+
* @returns {Promise}
102+
*/
103+
104+
}, {
105+
key: 'importKey',
106+
value: function importKey(key) {
107+
var jwk = Object.assign({}, key);
108+
var algorithm = this.params;
109+
var usages = key['key_ops'] || [];
110+
111+
if (key.use === 'sig') {
112+
usages.push('verify');
113+
}
114+
115+
if (key.use === 'enc') {
116+
// TODO: handle encryption keys
117+
return Promise.resolve(key);
118+
}
119+
120+
if (key.key_ops) {
121+
usages = key.key_ops;
122+
}
123+
124+
return crypto.subtle.importKey('jwk', jwk, algorithm, true, usages).then(function (cryptoKey) {
125+
Object.defineProperty(jwk, 'cryptoKey', {
126+
enumerable: false,
127+
value: cryptoKey
128+
});
129+
130+
return jwk;
131+
});
132+
}
133+
}]);
134+
135+
return RSASSA_PKCS1_v1_5;
136+
}();
137+
138+
/**
139+
* Export
140+
*/
141+
142+
143+
module.exports = RSASSA_PKCS1_v1_5;

0 commit comments

Comments
 (0)