|
|
| version 1.62, 2012/09/13 22:44:36 | version 1.63, 2012/09/15 20:51:10 |
|---|---|
| Line 343 void memxor(char *dest, const char *src, | Line 343 void memxor(char *dest, const char *src, |
| } | } |
| static void _digest(Request& r, MethodParams& params) { | static void _digest(Request& r, MethodParams& params) { |
| const String &sformat = params.as_string(0, PARAMETER_MUST_BE_STRING); | const String &smethod = params.as_string(0, PARAMETER_MUST_BE_STRING); |
| const char *string = params.as_string(1, PARAMETER_MUST_BE_STRING).cstr(); | const char *string = params.as_string(1, PARAMETER_MUST_BE_STRING).cstr(); |
| enum Format { F_MD5, F_SHA1 } format; | enum Method { M_MD5, M_SHA1 } method; |
| if (sformat == "md5") format = F_MD5; | if (smethod == "md5") method = M_MD5; |
| else if (sformat == "sha1" ) format = F_SHA1; | else if (smethod == "sha1" ) method = M_SHA1; |
| else throw Exception(PARSER_RUNTIME, &sformat, "must be 'md5' or 'sha1'"); | else throw Exception(PARSER_RUNTIME, &smethod, "must be 'md5' or 'sha1'"); |
| const char *hmac=0; | const char *hmac=0; |
| enum Encode { E_HEX, E_BASE64 } encode = E_HEX; | enum Format { F_HEX, F_BASE64 } 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 362 static void _digest(Request& r, MethodPa | Line 362 static void _digest(Request& r, MethodPa |
| hmac=value->as_string().cstr(); | hmac=value->as_string().cstr(); |
| valid_options++; | valid_options++; |
| } | } |
| if(Value* value=options->get("encode")) { | if(Value* value=options->get("format")) { |
| const String& sencode=value->as_string(); | const String& sformat=value->as_string(); |
| if (sencode == "hex") encode = E_HEX; | if (sformat == "hex") format = F_HEX; |
| else if (sencode == "base64" ) encode = E_BASE64; | else if (sformat == "base64" ) format = F_BASE64; |
| else throw Exception(PARSER_RUNTIME, &sencode, "must be 'hex' or 'base64'"); | 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()) |
| Line 375 static void _digest(Request& r, MethodPa | Line 375 static void _digest(Request& r, MethodPa |
| String::C digest; | String::C digest; |
| if(format == F_MD5){ | if(method == M_MD5){ |
| PA_MD5_CTX context; | PA_MD5_CTX context; |
| pa_MD5Init(&context); | pa_MD5Init(&context); |
| pa_MD5Update(&context, (const unsigned char*)string, strlen(string)); | pa_MD5Update(&context, (const unsigned char*)string, strlen(string)); |
| Line 385 static void _digest(Request& r, MethodPa | Line 385 static void _digest(Request& r, MethodPa |
| digest = String::C(str, 16); | digest = String::C(str, 16); |
| } | } |
| if(format == F_SHA1){ | if(method == M_SHA1){ |
| SHA1Context c; | SHA1Context c; |
| if(hmac){ | if(hmac){ |
| size_t keylen=strlen(hmac); | size_t keylen=strlen(hmac); |
| Line 428 static void _digest(Request& r, MethodPa | Line 428 static void _digest(Request& r, MethodPa |
| digest = String::C(str, 20); | digest = String::C(str, 20); |
| } | } |
| if(encode == E_HEX){ | if(format == F_HEX){ |
| r.write_pass_lang(*new String(hex_string((unsigned char *)digest.str, digest.length, false))); | r.write_pass_lang(*new String(hex_string((unsigned char *)digest.str, digest.length, false))); |
| } | } |
| if(encode == E_BASE64){ | if(format == F_BASE64){ |
| r.write_pass_lang(*new String(pa_base64_encode(digest.str, digest.length))); | r.write_pass_lang(*new String(pa_base64_encode(digest.str, digest.length))); |
| } | } |
| } | } |
| Line 544 MMath::MMath(): Methoded("math") { | Line 544 MMath::MMath(): Methoded("math") { |
| // ^math:sha1[string] | // ^math:sha1[string] |
| ADD1(sha1); | ADD1(sha1); |
| // ^math:digest[format;string;options] | // ^math:digest[method;string;options] |
| add_native_method("digest", Method::CT_STATIC, _digest, 2, 3); | add_native_method("digest", Method::CT_STATIC, _digest, 2, 3); |
| // ^math:crc32[string] | // ^math:crc32[string] |