From 7ef79ac12ab4e0b215a67a5760356ba812bb3ae3 Mon Sep 17 00:00:00 2001 From: visor Date: Wed, 12 Jun 2013 23:52:04 +0900 Subject: [PATCH] add hash functions. --- modules/ml-security.cc | 189 ++++++++++++++++++++++++++++++++++++++----------- modules/ml-security.h | 4 ++ wiki/wikiline.cc | 4 +- 3 files changed, 153 insertions(+), 44 deletions(-) diff --git a/modules/ml-security.cc b/modules/ml-security.cc index da0d157..ed01aaa 100644 --- a/modules/ml-security.cc +++ b/modules/ml-security.cc @@ -9,8 +9,11 @@ #include "ustring.h" #include #include +#include #include +#define S4K 4096 + /*DOC: ==cryptographic functions @@ -117,14 +120,7 @@ MNode* ml_hmac_sha256 (MNode* cell, MlEnv* mlenv) { return newMNode_str (new ustring (ans)); } -/*DOC: -===md5-string=== - (md5-string TEXT [#hex | #base64 | :hex BOOL | :base64 BOOL]) -> STRING - -*/ -//#AFUNC md5-string ml_md5_string -//#WIKIFUNC md5-string -MNode* ml_md5_string (MNode* cell, MlEnv* mlenv) { +static MNode* ml_hash_string (MNode* cell, MlEnv* mlenv, ustring fn (const ustring& text)) { MNode* arg = cell->cdr (); ustring text; enum { @@ -147,35 +143,21 @@ MNode* ml_md5_string (MNode* cell, MlEnv* mlenv) { if (keywords[1] && eval_bool (keywords[1], mlenv)) // base64 encode = ENC_BASE64; - { - MD5_CTX ctx; - u_char buf[16]; - - MD5Init (&ctx); - MD5Update (&ctx, text.c_str (), text.length ()); - MD5Final (buf, &ctx); - ans.assign (char_type (buf), 16); - switch (encode) { - case ENC_HEX: - ans = hexEncode (ans); - break; - case ENC_BASE64: - ans = base64Encode (ans.begin (), ans.end ()); - break; - default: - assert (0); - } + ans = fn (text); + switch (encode) { + case ENC_HEX: + ans = hexEncode (ans); + break; + case ENC_BASE64: + ans = base64Encode (ans.begin (), ans.end ()); + break; + default: + assert (0); } return newMNode_str (new ustring (ans)); } -/*DOC: -===md5-file=== - (md5-file FILENAME [#serial | #named | #static | :serial BOOL | :named BOOL | :static BOOL] [#hex | #base64 | :hex BOOL | :base64 BOOL]) -> STRING - -*/ -//#AFUNC md5-file ml_md5_file -MNode* ml_md5_file (MNode* cell, MlEnv* mlenv) { +static MNode* ml_hash_file (MNode* cell, MlEnv* mlenv, ustring fn (FileMacro& f)) { MNode* arg = cell->cdr (); ustring name; ustring src; @@ -211,18 +193,10 @@ MNode* ml_md5_file (MNode* cell, MlEnv* mlenv) { src = storetype.src (mlenv, name); if (src.length () > 0) { - MD5_CTX ctx; FileMacro f; - ssize_t len; - u_char buf[1024]; - MD5Init (&ctx); if (f.openRead (src.c_str ())) { - while ((len = f.read (buf, 1024)) > 0) { - MD5Update (&ctx, buf, len); - } - MD5Final (buf, &ctx); - ans.assign (char_type (buf), 16); + ans = fn (f); switch (encode) { case ENC_HEX: ans = hexEncode (ans); @@ -239,3 +213,134 @@ MNode* ml_md5_file (MNode* cell, MlEnv* mlenv) { return NULL; } +static ustring fn_md5 (const ustring& text) { + MD5_CTX ctx; + u_char buf[16]; + + MD5Init (&ctx); + MD5Update (&ctx, text.c_str (), text.length ()); + MD5Final (buf, &ctx); + return ustring (char_type (buf), 16); +} + +static ustring fn_md5_file (FileMacro& f) { + MD5_CTX ctx; + ssize_t len; + u_char buf[S4K]; + + MD5Init (&ctx); + while ((len = f.read (buf, S4K)) > 0) { + MD5Update (&ctx, buf, len); + } + MD5Final (buf, &ctx); + return ustring (char_type (buf), 16); +} + +static ustring fn_sha1 (const ustring& text) { + SHA_CTX ctx; + u_char buf[SHA_DIGEST_LENGTH]; + + SHA1_Init (&ctx); + SHA1_Update (&ctx, text.c_str (), text.length ()); + SHA1_Final (buf, &ctx); + return ustring (char_type (buf), SHA_DIGEST_LENGTH); +} + +static ustring fn_sha1_file (FileMacro& f) { + SHA_CTX ctx; + ssize_t len; + u_char buf[S4K]; + + SHA1_Init (&ctx); + while ((len = f.read (buf, S4K)) > 0) { + SHA1_Update (&ctx, buf, len); + } + SHA1_Final (buf, &ctx); + return ustring (char_type (buf), SHA_DIGEST_LENGTH); +} + +static ustring fn_sha256 (const ustring& text) { + SHA256_CTX ctx; + u_char buf[32]; + + SHA256_Init (&ctx); + SHA256_Update (&ctx, text.c_str (), text.length ()); + SHA256_Final (buf, &ctx); + return ustring (char_type (buf), 32); +} + +static ustring fn_sha256_file (FileMacro& f) { + SHA256_CTX ctx; + ssize_t len; + u_char buf[S4K]; + + SHA256_Init (&ctx); + while ((len = f.read (buf, S4K)) > 0) { + SHA256_Update (&ctx, buf, len); + } + SHA256_Final (buf, &ctx); + return ustring (char_type (buf), 32); +} + +/*DOC: +===md5-string=== + (md5-string TEXT [#hex | #base64 | :hex BOOL | :base64 BOOL]) -> STRING + +*/ +//#AFUNC md5-string ml_md5_string +//#WIKIFUNC md5-string +MNode* ml_md5_string (MNode* cell, MlEnv* mlenv) { + return ml_hash_string (cell, mlenv, fn_md5); +} + +/*DOC: +===md5-file=== + (md5-file FILENAME [#serial | #named | #static | :serial BOOL | :named BOOL | :static BOOL] [#hex | #base64 | :hex BOOL | :base64 BOOL]) -> STRING + +*/ +//#AFUNC md5-file ml_md5_file +MNode* ml_md5_file (MNode* cell, MlEnv* mlenv) { + return ml_hash_file (cell, mlenv, fn_md5_file); +} + +/*DOC: +===sha1-string=== + (sha1-string TEXT [#hex | #base64 | :hex BOOL | :base64 BOOL]) -> STRING + +*/ +//#AFUNC sha1-string ml_sha1_string +//#WIKIFUNC sha1-string +MNode* ml_sha1_string (MNode* cell, MlEnv* mlenv) { + return ml_hash_string (cell, mlenv, fn_sha1); +} + +/*DOC: +===sha1-file=== + (sha1-file FILENAME [#serial | #named | #static | :serial BOOL | :named BOOL | :static BOOL] [#hex | #base64 | :hex BOOL | :base64 BOOL]) -> STRING + +*/ +//#AFUNC sha1-file ml_sha1_file +MNode* ml_sha1_file (MNode* cell, MlEnv* mlenv) { + return ml_hash_file (cell, mlenv, fn_sha1_file); +} + +/*DOC: +===sha256-string=== + (sha256-string TEXT [#hex | #base64 | :hex BOOL | :base64 BOOL]) -> STRING + +*/ +//#AFUNC sha256-string ml_sha256_string +//#WIKIFUNC sha256-string +MNode* ml_sha256_string (MNode* cell, MlEnv* mlenv) { + return ml_hash_string (cell, mlenv, fn_sha256); +} + +/*DOC: +===sha256-file=== + (sha256-file FILENAME [#serial | #named | #static | :serial BOOL | :named BOOL | :static BOOL] [#hex | #base64 | :hex BOOL | :base64 BOOL]) -> STRING + +*/ +//#AFUNC sha256-file ml_sha256_file +MNode* ml_sha256_file (MNode* cell, MlEnv* mlenv) { + return ml_hash_file (cell, mlenv, fn_sha256_file); +} diff --git a/modules/ml-security.h b/modules/ml-security.h index 57bab2b..2ef5c77 100644 --- a/modules/ml-security.h +++ b/modules/ml-security.h @@ -11,5 +11,9 @@ MNode* ml_hmac_sha1 (MNode* cell, MlEnv* mlenv); MNode* ml_hmac_sha256 (MNode* cell, MlEnv* mlenv); MNode* ml_md5_string (MNode* cell, MlEnv* mlenv); MNode* ml_md5_file (MNode* cell, MlEnv* mlenv); +MNode* ml_sha1_string (MNode* cell, MlEnv* mlenv); +MNode* ml_sha1_file (MNode* cell, MlEnv* mlenv); +MNode* ml_sha256_string (MNode* cell, MlEnv* mlenv); +MNode* ml_sha256_file (MNode* cell, MlEnv* mlenv); #endif /* ML_SECURITY_H */ diff --git a/wiki/wikiline.cc b/wiki/wikiline.cc index 3647c9f..f195f77 100644 --- a/wiki/wikiline.cc +++ b/wiki/wikiline.cc @@ -300,8 +300,8 @@ bool wl_link_new (WikiMotorObjVecVec* args, WikiMotorObjVec* arg2, WikiMotorObj |'''onfocus='''''Link''||onfocus属性。Javascriptリンク。| |'''onblur='''''Link''||onblur属性。Javascriptリンク。| |'''onchange='''''Link''||onchange属性。Javascriptリンク。| -|'''width='''''Size''|'''w='''|width属性。単位はpx, pt, in, mm, cm, em, ex。または%。| -|'''height='''''Size''|'''h='''|height属性。単位はpx, pt, in, mm, cm, em, ex。または%。| +|'''width='''''Size''|'''w='''|styleのwidth属性。単位はpx, pt, in, mm, cm, em, ex。または%。| +|'''height='''''Size''|'''h='''|styleのheight属性。単位はpx, pt, in, mm, cm, em, ex。または%。| |'''alt='''''Text''||alt属性とlongdesc属性の指定。altオプションを指定すると,第二パラメータがlongdesc属性になる。| */ -- 2.11.0