|
|
| version 1.115, 2026/11/02 00:45:58 | version 1.116, 2026/11/04 13:40:42 |
|---|---|
| Line 19 | Line 19 |
| #include "pa_md5.h" | #include "pa_md5.h" |
| #include "pa_sha1.h" | #include "pa_sha1.h" |
| #include "pa_sha2.h" | #include "pa_sha2.h" |
| #include "pa_xxhash.h" | |
| #include "pa_random.h" | #include "pa_random.h" |
| #ifdef HAVE_CRYPT | #ifdef HAVE_CRYPT |
| Line 274 void memxor(char *dest, const char *src, | Line 275 void memxor(char *dest, const char *src, |
| update(&c, (const unsigned char*)tempdigest, digestlen); \ | update(&c, (const unsigned char*)tempdigest, digestlen); \ |
| } | } |
| enum Format { F_HEX, F_BASE64, F_FILE }; | |
| static Format as_format(const String& sformat) { | |
| if (sformat == "hex") return F_HEX; | |
| if (sformat == "base64" ) return F_BASE64; | |
| if (sformat == "file" ) return F_FILE; | |
| throw Exception(PARSER_RUNTIME, &sformat, "must be 'hex' or 'base64'"); | |
| } | |
| static void write_digest(Request& r, const String::C& digest, Format format) { | |
| if(format == F_HEX){ | |
| r.write(*new String(hex_string((unsigned char *)digest.str, digest.length, false))); | |
| } | |
| if(format == F_BASE64){ | |
| r.write(*new String(pa_base64_encode(digest.str, digest.length, Base64Options(false /*no wrap*/)))); | |
| } | |
| if(format == F_FILE){ | |
| VFile& result=*new VFile; | |
| // the file keeps the pointer, and the digest is in the caller's stack | |
| result.set_binary(true, pa_strdup(digest.str, digest.length), digest.length); | |
| r.write(result); | |
| } | |
| } | |
| static void _digest(Request& r, MethodParams& params) { | static void _digest(Request& r, MethodParams& params) { |
| const String &smethod = params.as_string(0, PARAMETER_MUST_BE_STRING); | const String &smethod = params.as_string(0, PARAMETER_MUST_BE_STRING); |
| Line 290 static void _digest(Request& r, MethodPa | Line 315 static void _digest(Request& r, MethodPa |
| const char *hmac=0; | const char *hmac=0; |
| size_t hmac_len=0; | size_t hmac_len=0; |
| enum Format { F_HEX, F_BASE64, F_FILE } format = F_HEX; | Format format = F_HEX; |
| if(params.count() == 3) | if(params.count() == 3) |
| if(HashStringValue* options=params.as_hash(2)) { | if(HashStringValue* options=params.as_hash(2)) { |
| Line 306 static void _digest(Request& r, MethodPa | Line 331 static void _digest(Request& r, MethodPa |
| valid_options++; | valid_options++; |
| } | } |
| if(Value* value=options->get("format")) { | if(Value* value=options->get("format")) { |
| const String& sformat=value->as_string(); | format = as_format(value->as_string()); |
| if (sformat == "hex") format = F_HEX; | |
| else if (sformat == "base64" ) format = F_BASE64; | |
| else if (sformat == "file" ) format = F_FILE; | |
| else throw Exception(PARSER_RUNTIME, &sformat, "must be 'hex' or 'base64'"); | |
| valid_options++; | valid_options++; |
| } | } |
| if(valid_options!=options->count()) | if(valid_options!=options->count()) |
| throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); | throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); |
| } | } |
| char str[SHA512_DIGEST_LENGTH]; // the longest digest | |
| String::C digest; | String::C digest; |
| if(method == M_MD5){ | if(method == M_MD5){ |
| Line 327 static void _digest(Request& r, MethodPa | Line 349 static void _digest(Request& r, MethodPa |
| pa_MD5Init(&c); | pa_MD5Init(&c); |
| pa_MD5Update(&c, (const unsigned char*)data.str, data.length); | pa_MD5Update(&c, (const unsigned char*)data.str, data.length); |
| } | } |
| char *str=(char *)pa_malloc(16); | |
| pa_MD5Final((unsigned char *)str, &c); | pa_MD5Final((unsigned char *)str, &c); |
| digest = String::C(str, 16); | digest = String::C(str, 16); |
| } | } |
| Line 340 static void _digest(Request& r, MethodPa | Line 361 static void _digest(Request& r, MethodPa |
| pa_SHA1Reset(&c); | pa_SHA1Reset(&c); |
| pa_SHA1Input(&c, (const unsigned char*)data.str, data.length); | pa_SHA1Input(&c, (const unsigned char*)data.str, data.length); |
| } | } |
| char *str=(char *)pa_malloc(20); | |
| pa_SHA1ReadDigest(str, &c); | pa_SHA1ReadDigest(str, &c); |
| digest = String::C(str, 20); | digest = String::C(str, 20); |
| } | } |
| Line 353 static void _digest(Request& r, MethodPa | Line 373 static void _digest(Request& r, MethodPa |
| pa_SHA256_Init(&c); | pa_SHA256_Init(&c); |
| pa_SHA256_Update(&c, (const unsigned char*)data.str, data.length); | pa_SHA256_Update(&c, (const unsigned char*)data.str, data.length); |
| } | } |
| char *str=(char *)pa_malloc(SHA256_DIGEST_LENGTH); | |
| pa_SHA256_Final((unsigned char *)str, &c); | pa_SHA256_Final((unsigned char *)str, &c); |
| digest = String::C(str, SHA256_DIGEST_LENGTH); | digest = String::C(str, SHA256_DIGEST_LENGTH); |
| } | } |
| Line 366 static void _digest(Request& r, MethodPa | Line 385 static void _digest(Request& r, MethodPa |
| pa_SHA512_Init(&c); | pa_SHA512_Init(&c); |
| pa_SHA512_Update(&c, (const unsigned char*)data.str, data.length); | pa_SHA512_Update(&c, (const unsigned char*)data.str, data.length); |
| } | } |
| char *str=(char *)pa_malloc(SHA512_DIGEST_LENGTH); | |
| pa_SHA512_Final((unsigned char *)str, &c); | pa_SHA512_Final((unsigned char *)str, &c); |
| digest = String::C(str, SHA512_DIGEST_LENGTH); | digest = String::C(str, SHA512_DIGEST_LENGTH); |
| } | } |
| if(format == F_HEX){ | write_digest(r, digest, format); |
| r.write(*new String(hex_string((unsigned char *)digest.str, digest.length, false))); | } |
| } | |
| if(format == F_BASE64){ | static void _xxhash(Request& r, MethodParams& params) { |
| r.write(*new String(pa_base64_encode(digest.str, digest.length, Base64Options(false /*no wrap*/)))); | const String &salgo = params.as_string(0, PARAMETER_MUST_BE_STRING); |
| } | |
| if(format == F_FILE){ | String::C data=getData(params.as_no_junction(1, "parameter must be string or file"), r); |
| VFile& result=*new VFile; | |
| result.set_binary(true, digest.str, digest.length); | bool is_xxh64; |
| r.write(result); | |
| } | if (salgo == "xxh32" || salgo == "32") is_xxh64 = false; |
| else if (salgo == "xxh64" || salgo == "64") is_xxh64 = true; | |
| else throw Exception(PARSER_RUNTIME, &salgo, "must be 'xxh32' or 'xxh64'"); | |
| Format format = F_HEX; | |
| if(params.count() == 3) | |
| if(HashStringValue* options=params.as_hash(2)) { | |
| int valid_options=0; | |
| if(Value* value=options->get("format")) { | |
| format = as_format(value->as_string()); | |
| valid_options++; | |
| } | |
| if(valid_options!=options->count()) | |
| throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); | |
| } | |
| size_t size=is_xxh64 ? 8 : 4; | |
| uint64_t hash=is_xxh64 ? pa_xxh64(data.str, data.length, 0) : pa_xxh32(data.str, data.length, 0); | |
| // canonical form, as xxhsum prints it: the most significant byte first | |
| char str[8]; | |
| for(size_t i=0; i<size; i++) | |
| str[i]=(char)(hash >> (8*(size-1-i))); | |
| write_digest(r, String::C(str, size), format); | |
| } | } |
| static void _uuid(Request& r, MethodParams& params) { | static void _uuid(Request& r, MethodParams& params) { |
| Line 680 MMath::MMath(): Methoded("math") { | Line 723 MMath::MMath(): Methoded("math") { |
| // ^math:digest[method;string|file;options] | // ^math:digest[method;string|file;options] |
| add_native_method("digest", Method::CT_STATIC, _digest, 2, 3); | add_native_method("digest", Method::CT_STATIC, _digest, 2, 3); |
| // ^math:xxhash[method;string|file;options] | |
| add_native_method("xxhash", Method::CT_STATIC, _xxhash, 2, 3); | |
| // ^math:crc32[string] | // ^math:crc32[string] |
| ADD1(crc32); | ADD1(crc32); |