1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-02-28 08:50:01 +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
*/ */