1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-06-18 11:29:55 +00:00

Add sha256() function to $tw.utils (#8043)

This commit is contained in:
Mario Pietsch 2024-03-08 18:34:30 +01:00 committed by GitHub
parent e64aa6c8f9
commit 28791287b2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 7 deletions

View File

@ -14,12 +14,9 @@ Filter operators for cryptography, using the Stanford JavaScript library
exports.sha256 = function(source,operator,options) { exports.sha256 = function(source,operator,options) {
var results = [], var results = [],
length = parseInt(operator.operand,10) || 20, length = parseInt(operator.operand,10) || 20;
sha256 = function(text) {
return $tw.sjcl.codec.hex.fromBits($tw.sjcl.hash.sha256.hash(text)).substr(0,length);
};
source(function(tiddler,title) { source(function(tiddler,title) {
results.push(sha256(title)); results.push($tw.utils.sha256(title,{length: length}));
}); });
return results; return results;
}; };

View File

@ -819,6 +819,15 @@ exports.hashString = function(str) {
},0); },0);
}; };
/*
Cryptographic hash function as used by sha256 filter operator
options.length .. number of characters returned defaults to 64
*/
exports.sha256 = function(str, options) {
options = options || {}
return sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(str)).substr(0,options.length || 64);
}
/* /*
Base64 utility functions that work in either browser or Node.js Base64 utility functions that work in either browser or Node.js
*/ */
@ -922,7 +931,7 @@ IE does not have sign function
*/ */
exports.sign = Math.sign || function(x) { exports.sign = Math.sign || function(x) {
x = +x; // convert to a number x = +x; // convert to a number
if (x === 0 || isNaN(x)) { if(x === 0 || isNaN(x)) {
return x; return x;
} }
return x > 0 ? 1 : -1; return x > 0 ? 1 : -1;
@ -935,7 +944,7 @@ exports.strEndsWith = function(str,ending,position) {
if(str.endsWith) { if(str.endsWith) {
return str.endsWith(ending,position); return str.endsWith(ending,position);
} else { } else {
if (typeof position !== 'number' || !isFinite(position) || Math.floor(position) !== position || position > str.length) { if(typeof position !== 'number' || !isFinite(position) || Math.floor(position) !== position || position > str.length) {
position = str.length; position = str.length;
} }
position -= ending.length; position -= ending.length;