瀏覽代碼

* Add SHA256

Bryan Drewery 16 年之前
父節點
當前提交
e80520fe2f
共有 6 個文件被更改,包括 71 次插入5 次删除
  1. 2 0
      doc/UPDATES
  2. 10 5
      doc/help.txt
  3. 12 0
      src/cmds.c
  4. 30 0
      src/crypt.c
  5. 3 0
      src/crypt.h
  6. 14 0
      src/mod/irc.mod/msgcmds.c

+ 2 - 0
doc/UPDATES

@@ -1,3 +1,5 @@
+* Added SHA256 support (cmd_sha256, Auth cmd: sha256)
+
 1.3 - http://wraith.botpack.net/milestone/1.3
 * Binary / shell / startup changes
   * Binary error messages are no longer obscure numbers or fake segfaults. (Compile with OBSCURE_ERRORS to re-enable)

+ 10 - 5
doc/help.txt

@@ -901,7 +901,7 @@ See also: down
 ###  $bdecrypt$b <key> <string>
    Decrypts the string using the specified key.
  
-See also: encrypt, randstring, md5, sha1
+See also: encrypt, randstring, md5, sha1, sha256
 :leaf:deluser
 ###  $bdeluser$b <nickname>
    Deletes a user record for a user on the channel, using their
@@ -968,7 +968,7 @@ See also: color, console, login, page, strip
 ###  $bencrypt$b <key> <string>
    Encrypts the string using the specified key.
  
-See also: decrypt, randstring, md5, sha1
+See also: decrypt, randstring, md5, sha1, sha256
 ::exec:
 ###  $bexec$b <params>
    The bot will execute the specified program with each param specified,
@@ -1271,7 +1271,7 @@ See also: match
 ###  $bmd5$b <string>
    Returns the MD5 hash of the specified string.
  
-See also: randstring, sha1, encrypt, decrypt
+See also: randstring, sha1, sha256, encrypt, decrypt
 ::me
 ###  $bme$b <text>
    Performs an action on the party line. This appears as "* bryan is leaving",
@@ -1450,7 +1450,7 @@ See also: color, console, echo, login, strip
 ###  $brandstring$b <len>
    Displays a random string of length 'len' up to 300 chars.
  
-See also: md5, sha1, encrypt, decrypt
+See also: md5, sha1, sha256, encrypt, decrypt
 ::rehash
 ###  $brehash$b 
    Don't use this cmd, it doesn't do what you think it does, and can result
@@ -1662,7 +1662,12 @@ See also: botset
 ###  $bsha1$b <string>
    Returns the SHA1 hash of the specified string.
  
-See also: randstring, md5, encrypt, decrypt
+See also: randstring, md5, sha256, encrypt, decrypt
+::sha256
+###  $bsha256$b <string>
+   Returns the SHA256 hash of the specified string.
+
+See also: randstring, md5, sha1, encrypt, decrypt
 ::simul
 ###  $bsimul$b <handle> <text>
    This allows you to simulate the specified handle typing the given text.

+ 12 - 0
src/cmds.c

@@ -1634,6 +1634,17 @@ static void cmd_sha1(int idx, char *par)
   dprintf(idx, "SHA1(%s) = %s\n", par, SHA1(par));
 }
 
+static void cmd_sha256(int idx, char *par)
+{
+  if (!par[0]) {
+    dprintf(idx, "Usage: sha256 <string>\n");
+    return;
+  }
+
+  putlog(LOG_CMDS, "*", "#%s# sha256 ...", dcc[idx].nick);
+  dprintf(idx, "SHA256(%s) = %s\n", par, SHA256(par));
+}
+
 static void cmd_conf(int idx, char *par)
 {
   if (!conf.bot->localhub && !conf.bot->hub) {
@@ -4569,6 +4580,7 @@ cmd_t C_dcc[] =
   {"randstring", 	"", 	(Function) cmd_randstring, 	NULL, AUTH_ALL},
   {"md5",		"",	(Function) cmd_md5,		NULL, AUTH_ALL},
   {"sha1",		"",	(Function) cmd_sha1,		NULL, AUTH_ALL},
+  {"sha256",		"",	(Function) cmd_sha256,		NULL, AUTH_ALL},
   {"conf",		"a",	(Function) cmd_conf,		NULL, 0},
   {"encrypt",		"",	(Function) cmd_encrypt,		NULL, AUTH_ALL},
   {"decrypt",		"",	(Function) cmd_decrypt,		NULL, AUTH_ALL},

+ 30 - 0
src/crypt.c

@@ -236,6 +236,36 @@ int sha1cmp(const char *hash, const char *string) {
   return n;
 }
 
+char *SHA256(const char *string)
+{
+  static int n = 0;
+  static char ret[5][SHA256_HASH_LENGTH + 1];
+  //Cleanse the current buffer
+  if (!string) {
+    OPENSSL_cleanse(ret[n], SHA256_HASH_LENGTH + 1);
+    return NULL;
+  }
+  char* sha256string = ret[n++];
+  unsigned char   sha256out[SHA256_HASH_LENGTH + 1] = "";
+  SHA256_CTX ctx;
+
+  SHA256_Init(&ctx);
+  SHA256_Update(&ctx, string, strlen(string));
+  SHA256_Final(sha256out, &ctx);
+  btoh(sha256out, SHA256_DIGEST_LENGTH, sha256string, SHA256_HASH_LENGTH + 1);
+  OPENSSL_cleanse(&ctx, sizeof(ctx));
+
+  if (n == 5) n = 0;
+
+  return sha256string;
+}
+
+int sha256cmp(const char *hash, const char *string) {
+  int n = strcmp(hash, SHA256(string));
+  SHA256(NULL);
+  return n;
+}
+
 void btoh(const unsigned char *md, size_t md_len, char *buf, const size_t buf_len)
 {
 #define doblock(n) simple_snprintf(&(buf[(i + n) << 1]), buf_len - ((i + n) << 1), "%02x", md[i + n]);

+ 3 - 0
src/crypt.h

@@ -16,6 +16,7 @@ namespace bd {
 };
 
 #define SHA_HASH_LENGTH (SHA_DIGEST_LENGTH << 1)
+#define SHA256_HASH_LENGTH (SHA256_DIGEST_LENGTH << 1)
 #define MD5_HASH_LENGTH (MD5_DIGEST_LENGTH << 1)
 
 #define SHA1_SALT_LEN 5
@@ -25,6 +26,8 @@ char *MD5(const char *);
 int md5cmp(const char *, const char*);
 char *SHA1(const char *);
 int sha1cmp(const char *, const char*);
+char *SHA256(const char *);
+int sha256cmp(const char *, const char*);
 
 char *encrypt_string(const char *, char *);
 bd::String encrypt_string(const bd::String&, const bd::String&);

+ 14 - 0
src/mod/irc.mod/msgcmds.c

@@ -654,6 +654,19 @@ static int msgc_sha1(Auth *a, char *chname, char *par)
   return BIND_RET_BREAK;
 }
 
+static int msgc_sha256(Auth *a, char *chname, char *par)
+{
+  struct chanset_t *chan = NULL;
+
+  LOGC("SHA256");
+
+  if (chname && chname[0])
+    chan = findchan_by_dname(chname);
+
+  reply(a->nick, chan, "SHA256(%s) = %s\n", par, SHA256(par));
+  return BIND_RET_BREAK;
+}
+
 static int msgc_invite(Auth *a, char *chname, char *par)
 {
   struct chanset_t *chan = NULL;
@@ -713,6 +726,7 @@ static cmd_t C_msgc[] =
   {"md5",		"",	(Function) msgc_md5,		NULL, LEAF|AUTH_MSG|AUTH_CHAN},
   {"op",		"",	(Function) msgc_op,		NULL, LEAF|AUTH_CHAN|AUTH_MSG},
   {"sha1",		"",	(Function) msgc_sha1,		NULL, LEAF|AUTH_CHAN|AUTH_MSG},
+  {"sha256",		"",	(Function) msgc_sha256,		NULL, LEAF|AUTH_CHAN|AUTH_MSG},
   {"voice",		"",	(Function) msgc_voice,		NULL, LEAF|AUTH_CHAN|AUTH_MSG},
   {NULL,		NULL,	NULL,				NULL, 0}
 };