Преглед на файлове

hmac compatible cpython input parameters

dreamcmi преди 3 години
родител
ревизия
a8bd50cfae

+ 3 - 3
examples/hmac/test_hmac.py

@@ -3,14 +3,14 @@ import hmac
 secret = "0123456789"
 payload = "helloworld"
 
-h = hmac.new(secret.encode(),digestmod="hmac-md5")
+h = hmac.new(secret.encode(),digestmod="md5")
 h.update(payload.encode())
 print("hmac-md5:",h.hexdigest())
 
-h = hmac.new(secret.encode(),digestmod="hmac-sha1")
+h = hmac.new(secret.encode(),digestmod="sha1")
 h.update(payload.encode())
 print("hmac-sha1:",h.hexdigest())
 
-h = hmac.new(secret.encode(),digestmod="hmac-sha256")
+h = hmac.new(secret.encode(),digestmod="sha256")
 h.update(payload.encode())
 print("hmac-sha256:",h.hexdigest())

+ 6 - 6
package/hmac/_hmac_HMAC.c

@@ -41,18 +41,18 @@ void _hmac_HMAC_new(PikaObj* self, Arg* key, Arg* msg, char* digestmod) {
     mbedtls_md_context_t ctx;
     mbedtls_md_init(&ctx);
 
-    if (strcmp(digestmod, "hmac-md5") == 0 ||
-        strcmp(digestmod, "HMAC-MD5") == 0) {
+    if (strcmp(digestmod, "md5") == 0 ||
+        strcmp(digestmod, "MD5") == 0) {
         mbedtls_md_setup(&ctx, mbedtls_md_info_from_type(MBEDTLS_MD_MD5), 1);
         obj_setInt(self, "_mode", PIKA_HMAC_MD5);
         init_buff(self, PIKA_HMAC_MD5);
-    } else if (strcmp(digestmod, "hmac-sha1") == 0 ||
-               strcmp(digestmod, "HMAC-SHA1") == 0) {
+    } else if (strcmp(digestmod, "sha1") == 0 ||
+               strcmp(digestmod, "SHA1") == 0) {
         mbedtls_md_setup(&ctx, mbedtls_md_info_from_type(MBEDTLS_MD_SHA1), 1);
         obj_setInt(self, "_mode", PIKA_HMAC_SHA1);
         init_buff(self, PIKA_HMAC_SHA1);
-    } else if (strcmp(digestmod, "hmac-sha256") == 0 ||
-               strcmp(digestmod, "HMAC-SHA256") == 0) {
+    } else if (strcmp(digestmod, "sha256") == 0 ||
+               strcmp(digestmod, "SHA256") == 0) {
         mbedtls_md_setup(&ctx, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 1);
         obj_setInt(self, "_mode", PIKA_HMAC_SHA256);
         init_buff(self, PIKA_HMAC_SHA256);

+ 1 - 1
package/hmac/hmac.py

@@ -1,6 +1,6 @@
 import _hmac
 
-def new(key:any, msg=None, digestmod="hmac-md5") -> _hmac.HMAC:
+def new(key:any, msg=None, digestmod="md5") -> _hmac.HMAC:
     hmac = _hmac.HMAC()
     hmac.new(key, msg, digestmod)
     return hmac

+ 1 - 1
port/linux/package/pikascript/hmac.py

@@ -1,6 +1,6 @@
 import _hmac
 
-def new(key:any, msg=None, digestmod="hmac-md5") -> _hmac.HMAC:
+def new(key:any, msg=None, digestmod="md5") -> _hmac.HMAC:
     hmac = _hmac.HMAC()
     hmac.new(key, msg, digestmod)
     return hmac

+ 6 - 6
port/linux/package/pikascript/pikascript-lib/hmac/_hmac_HMAC.c

@@ -41,18 +41,18 @@ void _hmac_HMAC_new(PikaObj* self, Arg* key, Arg* msg, char* digestmod) {
     mbedtls_md_context_t ctx;
     mbedtls_md_init(&ctx);
 
-    if (strcmp(digestmod, "hmac-md5") == 0 ||
-        strcmp(digestmod, "HMAC-MD5") == 0) {
+    if (strcmp(digestmod, "md5") == 0 ||
+        strcmp(digestmod, "MD5") == 0) {
         mbedtls_md_setup(&ctx, mbedtls_md_info_from_type(MBEDTLS_MD_MD5), 1);
         obj_setInt(self, "_mode", PIKA_HMAC_MD5);
         init_buff(self, PIKA_HMAC_MD5);
-    } else if (strcmp(digestmod, "hmac-sha1") == 0 ||
-               strcmp(digestmod, "HMAC-SHA1") == 0) {
+    } else if (strcmp(digestmod, "sha1") == 0 ||
+               strcmp(digestmod, "SHA1") == 0) {
         mbedtls_md_setup(&ctx, mbedtls_md_info_from_type(MBEDTLS_MD_SHA1), 1);
         obj_setInt(self, "_mode", PIKA_HMAC_SHA1);
         init_buff(self, PIKA_HMAC_SHA1);
-    } else if (strcmp(digestmod, "hmac-sha256") == 0 ||
-               strcmp(digestmod, "HMAC-SHA256") == 0) {
+    } else if (strcmp(digestmod, "sha256") == 0 ||
+               strcmp(digestmod, "SHA256") == 0) {
         mbedtls_md_setup(&ctx, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 1);
         obj_setInt(self, "_mode", PIKA_HMAC_SHA256);
         init_buff(self, PIKA_HMAC_SHA256);

+ 3 - 3
test/python/hmac/test_hmac.py

@@ -3,14 +3,14 @@ import hmac
 secret = "0123456789"
 payload = "helloworld"
 
-h = hmac.new(secret.encode(),digestmod="hmac-md5")
+h = hmac.new(secret.encode(),digestmod="md5")
 h.update(payload.encode())
 print("hmac-md5:",h.hexdigest())
 
-h = hmac.new(secret.encode(),digestmod="hmac-sha1")
+h = hmac.new(secret.encode(),digestmod="sha1")
 h.update(payload.encode())
 print("hmac-sha1:",h.hexdigest())
 
-h = hmac.new(secret.encode(),digestmod="hmac-sha256")
+h = hmac.new(secret.encode(),digestmod="sha256")
 h.update(payload.encode())
 print("hmac-sha256:",h.hexdigest())