Skip to content

Commit

Permalink
sign html
Browse files Browse the repository at this point in the history
  • Loading branch information
antelle committed Apr 2, 2016
1 parent 68a51a2 commit be2699f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
11 changes: 10 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,14 @@ module.exports = function(grunt) {
publicKey: 'app/resources/public-key.pem'
}
}
},
'sign-html': {
'app': {
options: {
file: 'dist/index.html',
privateKey: 'keys/private-key.pem'
}
}
}
});

Expand All @@ -464,7 +472,8 @@ module.exports = function(grunt) {
'inline',
'htmlmin',
'string-replace:manifest_html',
'string-replace:manifest'
'string-replace:manifest',
'sign-html'
]);

grunt.registerTask('desktop', [
Expand Down
1 change: 1 addition & 0 deletions app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<head lang="en">
<meta charset="UTF-8">
<title>KeeWeb</title>
<meta name="signature" content="">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="shortcut icon" href="favicon.png?__inline=true" />
<link rel="apple-touch-icon" sizes="192x192" href="touchicon.png?__inline=true">
Expand Down
26 changes: 26 additions & 0 deletions grunt/tasks/grunt-sign-html.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

module.exports = function (grunt) {
grunt.registerMultiTask('sign-html', 'Signs html page with a private key', function () {
var fs = require('fs');
var crypto = require('crypto');
if (!fs.existsSync(this.options().privateKey)) {
grunt.log.warn('Private key is missing, app html will not be signed.');
return;
}
var file = fs.readFileSync(this.options().file).toString('utf8');
var marker = '<meta name="signature" content="';
var ix = file.indexOf(marker);
if (ix < 0) {
grunt.warn('Signature placeholder not found');
return;
}
var sign = crypto.createSign('RSA-SHA256');
sign.write(file);
sign.end();
var privateKey = fs.readFileSync(this.options().privateKey, 'binary');
var signature = sign.sign(privateKey).toString('base64');
file = file.replace(marker, marker + signature);
fs.writeFileSync(this.options().file, file, 'utf8');
});
};

0 comments on commit be2699f

Please sign in to comment.