#include "ustring.h"
#include <md5.h>
#include <openssl/hmac.h>
+#include <openssl/sha.h>
#include <sys/types.h>
+#define S4K 4096
+
/*DOC:
==cryptographic functions
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 {
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;
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);
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);
+}