mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-15 17:02:53 +08:00
Merge branch 'add-hashlib'
This commit is contained in:
commit
6f7f5e8a26
1
.github/workflows/CI.yml
vendored
1
.github/workflows/CI.yml
vendored
@ -25,6 +25,7 @@ jobs:
|
||||
sudo apt install liblua5.3-dev
|
||||
sudo apt install valgrind
|
||||
sudo apt install upx
|
||||
sudo apt install libmbedtls-dev
|
||||
|
||||
- name: install_google_benchmark
|
||||
run: |
|
||||
|
1
.github/workflows/TEST.yml
vendored
1
.github/workflows/TEST.yml
vendored
@ -25,6 +25,7 @@ jobs:
|
||||
sudo apt install liblua5.3-dev
|
||||
sudo apt install valgrind
|
||||
sudo apt install upx
|
||||
sudo apt install libmbedtls-dev
|
||||
|
||||
- name: install_google_benchmark
|
||||
run: git clone --branch v1.6.1 https://github.com/google/benchmark.git && cd benchmark && cmake -E make_directory "build" && cmake -E chdir "build" cmake -DBENCHMARK_DOWNLOAD_DEPENDENCIES=on -DCMAKE_BUILD_TYPE=Release ../ && cmake --build "build" --config Release && sudo cmake --build "build" --config Release --target install
|
||||
|
@ -83,3 +83,4 @@ RUN code-server --install-extension llvm-vs-code-extensions.vscode-clangd
|
||||
RUN code-server --install-extension vscodevim.vim
|
||||
RUN code-server --install-extension zhuangtongfa.material-theme
|
||||
RUN apt-get install clangd -y
|
||||
RUN apt-get install libmbedtls-dev -y
|
||||
|
12
package/hashlib/_hashlib.pyi
Normal file
12
package/hashlib/_hashlib.pyi
Normal file
@ -0,0 +1,12 @@
|
||||
class Hash:
|
||||
def new(self, mode: str): ...
|
||||
def md5(self, data: any): ...
|
||||
def sha1(self, data: any): ...
|
||||
def sha224(self, data: any): ...
|
||||
def sha256(self, data: any): ...
|
||||
def sha384(self, data: any): ...
|
||||
def sha512(self, data: any): ...
|
||||
def update(self, data: any): ...
|
||||
def digest(self) -> any: ...
|
||||
def hexdigest(self) -> str: ...
|
||||
def copy(self): ...
|
279
package/hashlib/_hashlib_Hash.c
Normal file
279
package/hashlib/_hashlib_Hash.c
Normal file
@ -0,0 +1,279 @@
|
||||
#include "_hashlib_Hash.h"
|
||||
|
||||
#include "string.h"
|
||||
|
||||
#include "mbedtls/md5.h"
|
||||
#include "mbedtls/sha1.h"
|
||||
#include "mbedtls/sha256.h"
|
||||
#include "mbedtls/sha512.h"
|
||||
|
||||
enum {
|
||||
PIKA_HASHLIB_MD5 = 16,
|
||||
PIKA_HASHLIB_SHA1 = 20,
|
||||
PIKA_HASHLIB_SHA224 = 28,
|
||||
PIKA_HASHLIB_SHA256 = 32,
|
||||
PIKA_HASHLIB_SHA384 = 48,
|
||||
PIKA_HASHLIB_SHA512 = 64
|
||||
} pika_hashlib_t;
|
||||
|
||||
void to_hex(uint8_t* s, int l, uint8_t* d);
|
||||
|
||||
void _hashlib_Hash_md5(PikaObj* self, Arg* data) {
|
||||
size_t size = arg_getBytesSize(data);
|
||||
uint8_t* data_h = arg_getBytes(data);
|
||||
|
||||
mbedtls_md5_context context;
|
||||
mbedtls_md5_init(&context);
|
||||
mbedtls_md5_starts(&context);
|
||||
if (size != 0) {
|
||||
mbedtls_md5_update(&context, data_h, size);
|
||||
}
|
||||
obj_setStruct(self, "context", context);
|
||||
obj_setInt(self, "mode", PIKA_HASHLIB_MD5);
|
||||
return;
|
||||
}
|
||||
|
||||
void _hashlib_Hash_sha1(PikaObj* self, Arg* data) {
|
||||
size_t size = arg_getBytesSize(data);
|
||||
uint8_t* data_h = arg_getBytes(data);
|
||||
|
||||
mbedtls_sha1_context context;
|
||||
mbedtls_sha1_init(&context);
|
||||
mbedtls_sha1_starts(&context);
|
||||
if (size != 0) {
|
||||
mbedtls_sha1_update(&context, data_h, size);
|
||||
}
|
||||
obj_setStruct(self, "context", context);
|
||||
obj_setInt(self, "mode", PIKA_HASHLIB_SHA1);
|
||||
return;
|
||||
}
|
||||
|
||||
void _hashlib_Hash_sha224(PikaObj* self, Arg* data) {
|
||||
size_t size = arg_getBytesSize(data);
|
||||
uint8_t* data_h = arg_getBytes(data);
|
||||
|
||||
mbedtls_sha256_context context;
|
||||
mbedtls_sha256_init(&context);
|
||||
mbedtls_sha256_starts(&context, 1);
|
||||
if (size != 0) {
|
||||
mbedtls_sha256_update(&context, data_h, size);
|
||||
}
|
||||
obj_setStruct(self, "context", context);
|
||||
obj_setInt(self, "mode", PIKA_HASHLIB_SHA256);
|
||||
return;
|
||||
}
|
||||
|
||||
void _hashlib_Hash_sha256(PikaObj* self, Arg* data) {
|
||||
size_t size = arg_getBytesSize(data);
|
||||
uint8_t* data_h = arg_getBytes(data);
|
||||
|
||||
mbedtls_sha256_context context;
|
||||
mbedtls_sha256_init(&context);
|
||||
mbedtls_sha256_starts(&context, 0);
|
||||
if (size != 0) {
|
||||
mbedtls_sha256_update(&context, data_h, size);
|
||||
}
|
||||
obj_setStruct(self, "context", context);
|
||||
obj_setInt(self, "mode", PIKA_HASHLIB_SHA256);
|
||||
return;
|
||||
}
|
||||
|
||||
void _hashlib_Hash_sha384(PikaObj* self, Arg* data) {
|
||||
size_t size = arg_getBytesSize(data);
|
||||
uint8_t* data_h = arg_getBytes(data);
|
||||
|
||||
mbedtls_sha512_context context;
|
||||
mbedtls_sha512_init(&context);
|
||||
mbedtls_sha512_starts(&context, 1);
|
||||
if (size != 0) {
|
||||
mbedtls_sha512_update(&context, data_h, size);
|
||||
}
|
||||
obj_setStruct(self, "context", context);
|
||||
obj_setInt(self, "mode", PIKA_HASHLIB_SHA512);
|
||||
return;
|
||||
}
|
||||
|
||||
void _hashlib_Hash_sha512(PikaObj* self, Arg* data) {
|
||||
size_t size = arg_getBytesSize(data);
|
||||
uint8_t* data_h = arg_getBytes(data);
|
||||
|
||||
mbedtls_sha512_context context;
|
||||
mbedtls_sha512_init(&context);
|
||||
mbedtls_sha512_starts(&context, 0);
|
||||
if (size != 0) {
|
||||
mbedtls_sha512_update(&context, data_h, size);
|
||||
}
|
||||
obj_setStruct(self, "context", context);
|
||||
obj_setInt(self, "mode", PIKA_HASHLIB_SHA512);
|
||||
return;
|
||||
}
|
||||
|
||||
void _hashlib_Hash_new(PikaObj* self, char* mode) {
|
||||
if (strcmp(mode, "md5") || strcmp(mode, "MD5")) {
|
||||
mbedtls_md5_context context;
|
||||
mbedtls_md5_init(&context);
|
||||
mbedtls_md5_starts(&context);
|
||||
obj_setStruct(self, "context", context);
|
||||
obj_setInt(self, "mode", PIKA_HASHLIB_MD5);
|
||||
} else if (strcmp(mode, "sha1") || strcmp(mode, "SHA1")) {
|
||||
mbedtls_sha1_context context;
|
||||
mbedtls_sha1_init(&context);
|
||||
mbedtls_sha1_starts(&context);
|
||||
obj_setStruct(self, "context", context);
|
||||
obj_setInt(self, "mode", PIKA_HASHLIB_SHA1);
|
||||
} else if (strcmp(mode, "sha1") || strcmp(mode, "SHA1")) {
|
||||
mbedtls_sha1_context context;
|
||||
mbedtls_sha1_init(&context);
|
||||
mbedtls_sha1_starts(&context);
|
||||
obj_setStruct(self, "context", context);
|
||||
obj_setInt(self, "mode", PIKA_HASHLIB_SHA1);
|
||||
} else if (strcmp(mode, "sha224") || strcmp(mode, "sha224")) {
|
||||
mbedtls_sha256_context context;
|
||||
mbedtls_sha256_init(&context);
|
||||
mbedtls_sha256_starts(&context, 1);
|
||||
obj_setStruct(self, "context", context);
|
||||
obj_setInt(self, "mode", PIKA_HASHLIB_SHA256);
|
||||
} else if (strcmp(mode, "sha256") || strcmp(mode, "SHA224")) {
|
||||
mbedtls_sha256_context context;
|
||||
mbedtls_sha256_init(&context);
|
||||
mbedtls_sha256_starts(&context, 0);
|
||||
obj_setStruct(self, "context", context);
|
||||
obj_setInt(self, "mode", PIKA_HASHLIB_SHA256);
|
||||
} else if (strcmp(mode, "sha384") || strcmp(mode, "SHA384")) {
|
||||
mbedtls_sha512_context context;
|
||||
mbedtls_sha512_init(&context);
|
||||
mbedtls_sha512_starts(&context, 1);
|
||||
obj_setStruct(self, "context", context);
|
||||
obj_setInt(self, "mode", PIKA_HASHLIB_SHA384);
|
||||
} else if (strcmp(mode, "sha512") || strcmp(mode, "SHA512")) {
|
||||
mbedtls_sha512_context context;
|
||||
mbedtls_sha512_init(&context);
|
||||
mbedtls_sha512_starts(&context, 0);
|
||||
obj_setStruct(self, "context", context);
|
||||
obj_setInt(self, "mode", PIKA_HASHLIB_SHA512);
|
||||
} else {
|
||||
obj_setErrorCode(self, -1); // not support mode
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void _hashlib_Hash_update(PikaObj* self, Arg* data) {
|
||||
size_t size = arg_getBytesSize(data);
|
||||
uint8_t* data_h = arg_getBytes(data);
|
||||
|
||||
if (size != 0) {
|
||||
void* context = obj_getStruct(self, "context");
|
||||
switch (obj_getInt(self, "mode")) {
|
||||
case PIKA_HASHLIB_MD5:
|
||||
mbedtls_md5_update((mbedtls_md5_context*)context, data_h, size);
|
||||
break;
|
||||
case PIKA_HASHLIB_SHA1:
|
||||
mbedtls_sha1_update((mbedtls_sha1_context*)context, data_h,
|
||||
size);
|
||||
break;
|
||||
case PIKA_HASHLIB_SHA224:
|
||||
case PIKA_HASHLIB_SHA256:
|
||||
mbedtls_sha256_update((mbedtls_sha256_context*)context, data_h,
|
||||
size);
|
||||
break;
|
||||
case PIKA_HASHLIB_SHA384:
|
||||
case PIKA_HASHLIB_SHA512:
|
||||
mbedtls_sha512_update((mbedtls_sha512_context*)context, data_h,
|
||||
size);
|
||||
break;
|
||||
default:
|
||||
obj_setErrorCode(self, -1); // not support mode
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
obj_setErrorCode(self, -2); // io error
|
||||
}
|
||||
}
|
||||
|
||||
Arg* _hashlib_Hash_digest(PikaObj* self) {
|
||||
obj_setBytes(self, "buff", NULL, 64);
|
||||
uint8_t* buff = obj_getBytes(self, "buff");
|
||||
void* context = obj_getStruct(self, "context");
|
||||
|
||||
switch (obj_getInt(self, "mode")) {
|
||||
case PIKA_HASHLIB_MD5:
|
||||
mbedtls_md5_finish((mbedtls_md5_context*)context, buff);
|
||||
return arg_newBytes(buff, PIKA_HASHLIB_MD5);
|
||||
break;
|
||||
case PIKA_HASHLIB_SHA1:
|
||||
mbedtls_sha1_finish((mbedtls_sha1_context*)context, buff);
|
||||
return arg_newBytes(buff, PIKA_HASHLIB_SHA1);
|
||||
break;
|
||||
case PIKA_HASHLIB_SHA224:
|
||||
mbedtls_sha256_finish((mbedtls_sha256_context*)context, buff);
|
||||
return arg_newBytes(buff, PIKA_HASHLIB_SHA224);
|
||||
break;
|
||||
case PIKA_HASHLIB_SHA256:
|
||||
mbedtls_sha256_finish((mbedtls_sha256_context*)context, buff);
|
||||
return arg_newBytes(buff, PIKA_HASHLIB_SHA256);
|
||||
break;
|
||||
case PIKA_HASHLIB_SHA384:
|
||||
mbedtls_sha512_finish((mbedtls_sha512_context*)context, buff);
|
||||
return arg_newBytes(buff, PIKA_HASHLIB_SHA384);
|
||||
break;
|
||||
case PIKA_HASHLIB_SHA512:
|
||||
mbedtls_sha512_finish((mbedtls_sha512_context*)context, buff);
|
||||
return arg_newBytes(buff, PIKA_HASHLIB_SHA512);
|
||||
break;
|
||||
default:
|
||||
obj_setErrorCode(self, -1); // not support mode
|
||||
return arg_newNull();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
char* _hashlib_Hash_hexdigest(PikaObj* self) {
|
||||
obj_setBytes(self, "hexbuff", NULL, 128);
|
||||
uint8_t* hexbuff = obj_getBytes(self, "hexbuff");
|
||||
uint8_t* buff = obj_getBytes(self, "buff");
|
||||
|
||||
if (buff == NULL) {
|
||||
obj_setBytes(self, "buff", NULL, 64);
|
||||
uint8_t* buff = obj_getBytes(self, "buff");
|
||||
void* context = obj_getStruct(self, "context");
|
||||
|
||||
switch (obj_getInt(self, "mode")) {
|
||||
case PIKA_HASHLIB_MD5:
|
||||
mbedtls_md5_finish((mbedtls_md5_context*)context, buff);
|
||||
break;
|
||||
case PIKA_HASHLIB_SHA1:
|
||||
mbedtls_sha1_finish((mbedtls_sha1_context*)context, buff);
|
||||
break;
|
||||
case PIKA_HASHLIB_SHA224:
|
||||
mbedtls_sha256_finish((mbedtls_sha256_context*)context, buff);
|
||||
break;
|
||||
case PIKA_HASHLIB_SHA256:
|
||||
mbedtls_sha256_finish((mbedtls_sha256_context*)context, buff);
|
||||
break;
|
||||
case PIKA_HASHLIB_SHA384:
|
||||
mbedtls_sha512_finish((mbedtls_sha512_context*)context, buff);
|
||||
break;
|
||||
case PIKA_HASHLIB_SHA512:
|
||||
mbedtls_sha512_finish((mbedtls_sha512_context*)context, buff);
|
||||
break;
|
||||
default:
|
||||
obj_setErrorCode(self, -1); // not support mode
|
||||
break;
|
||||
}
|
||||
to_hex(buff, obj_getInt(self, "mode"), hexbuff);
|
||||
} else {
|
||||
to_hex(buff, obj_getInt(self, "mode"), hexbuff);
|
||||
}
|
||||
return (char*)hexbuff;
|
||||
}
|
||||
|
||||
void _hashlib_Hash_copy(PikaObj* self) {}
|
||||
|
||||
const uint8_t hex_table[] = {'0', '1', '2', '3', '4', '5', '6', '7',
|
||||
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
|
||||
void to_hex(uint8_t* s, int l, uint8_t* d) {
|
||||
while (l--) {
|
||||
*(d++) = hex_table[*s >> 4];
|
||||
*(d++) = hex_table[*(s++) & 0x0f];
|
||||
}
|
||||
}
|
43
package/hashlib/hashlib.py
Normal file
43
package/hashlib/hashlib.py
Normal file
@ -0,0 +1,43 @@
|
||||
import _hashlib
|
||||
|
||||
|
||||
def new(mode: str) -> _hashlib.Hash:
|
||||
hash = _hashlib.Hash()
|
||||
hash.new(mode)
|
||||
return hash
|
||||
|
||||
|
||||
def md5(data: bytes) -> _hashlib.Hash:
|
||||
hash = _hashlib.Hash()
|
||||
hash.md5(data)
|
||||
return hash
|
||||
|
||||
|
||||
def sha1(data: bytes) -> _hashlib.Hash:
|
||||
hash = _hashlib.Hash()
|
||||
hash.sha1(data)
|
||||
return hash
|
||||
|
||||
|
||||
def sha224(data: bytes) -> _hashlib.Hash:
|
||||
hash = _hashlib.Hash()
|
||||
hash.sha224(data)
|
||||
return hash
|
||||
|
||||
|
||||
def sha256(data: bytes) -> _hashlib.Hash:
|
||||
hash = _hashlib.Hash()
|
||||
hash.sha256(data)
|
||||
return hash
|
||||
|
||||
|
||||
def sha384(data: bytes) -> _hashlib.Hash:
|
||||
hash = _hashlib.Hash()
|
||||
hash.sha384(data)
|
||||
return hash
|
||||
|
||||
|
||||
def sha512(data: bytes) -> _hashlib.Hash:
|
||||
hash = _hashlib.Hash()
|
||||
hash.sha512(data)
|
||||
return hash
|
@ -1,11 +1,11 @@
|
||||
#设置 BINARY 为项目名IndexProject
|
||||
set(BINARY ${CMAKE_PROJECT_NAME})
|
||||
|
||||
set(CMAKE_C_FLAGS "-Wno-error=deprecated-declarations -Wno-deprecated-declarations")
|
||||
file(GLOB_RECURSE SOURCES LIST_DIRECTORIES true *.h *.c)
|
||||
set(SOURCES ${SOURCES})
|
||||
|
||||
# support <math.h>
|
||||
link_libraries(m lua5.3 pthread)
|
||||
link_libraries(m lua5.3 pthread mbedcrypto)
|
||||
|
||||
add_library(${BINARY}-core
|
||||
STATIC
|
||||
|
12
port/linux/package/pikascript/_hashlib.pyi
Normal file
12
port/linux/package/pikascript/_hashlib.pyi
Normal file
@ -0,0 +1,12 @@
|
||||
class Hash:
|
||||
def new(self, mode: str): ...
|
||||
def md5(self, data: any): ...
|
||||
def sha1(self, data: any): ...
|
||||
def sha224(self, data: any): ...
|
||||
def sha256(self, data: any): ...
|
||||
def sha384(self, data: any): ...
|
||||
def sha512(self, data: any): ...
|
||||
def update(self, data: any): ...
|
||||
def digest(self) -> any: ...
|
||||
def hexdigest(self) -> str: ...
|
||||
def copy(self): ...
|
43
port/linux/package/pikascript/hashlib.py
Normal file
43
port/linux/package/pikascript/hashlib.py
Normal file
@ -0,0 +1,43 @@
|
||||
import _hashlib
|
||||
|
||||
|
||||
def new(mode: str) -> _hashlib.Hash:
|
||||
hash = _hashlib.Hash()
|
||||
hash.new(mode)
|
||||
return hash
|
||||
|
||||
|
||||
def md5(data: bytes) -> _hashlib.Hash:
|
||||
hash = _hashlib.Hash()
|
||||
hash.md5(data)
|
||||
return hash
|
||||
|
||||
|
||||
def sha1(data: bytes) -> _hashlib.Hash:
|
||||
hash = _hashlib.Hash()
|
||||
hash.sha1(data)
|
||||
return hash
|
||||
|
||||
|
||||
def sha224(data: bytes) -> _hashlib.Hash:
|
||||
hash = _hashlib.Hash()
|
||||
hash.sha224(data)
|
||||
return hash
|
||||
|
||||
|
||||
def sha256(data: bytes) -> _hashlib.Hash:
|
||||
hash = _hashlib.Hash()
|
||||
hash.sha256(data)
|
||||
return hash
|
||||
|
||||
|
||||
def sha384(data: bytes) -> _hashlib.Hash:
|
||||
hash = _hashlib.Hash()
|
||||
hash.sha384(data)
|
||||
return hash
|
||||
|
||||
|
||||
def sha512(data: bytes) -> _hashlib.Hash:
|
||||
hash = _hashlib.Hash()
|
||||
hash.sha512(data)
|
||||
return hash
|
@ -5,6 +5,9 @@ import GTestTask, TempDevTest
|
||||
import cb_test
|
||||
import configparser
|
||||
import test_module1, test_cmodule, test_module4, import_test
|
||||
import hashlib
|
||||
|
||||
hashlib.sha256(b"123456")
|
||||
|
||||
mem = PikaStdLib.MemChecker()
|
||||
print('hello pikascript!')
|
||||
|
@ -0,0 +1,279 @@
|
||||
#include "_hashlib_Hash.h"
|
||||
|
||||
#include "string.h"
|
||||
|
||||
#include "mbedtls/md5.h"
|
||||
#include "mbedtls/sha1.h"
|
||||
#include "mbedtls/sha256.h"
|
||||
#include "mbedtls/sha512.h"
|
||||
|
||||
enum {
|
||||
PIKA_HASHLIB_MD5 = 16,
|
||||
PIKA_HASHLIB_SHA1 = 20,
|
||||
PIKA_HASHLIB_SHA224 = 28,
|
||||
PIKA_HASHLIB_SHA256 = 32,
|
||||
PIKA_HASHLIB_SHA384 = 48,
|
||||
PIKA_HASHLIB_SHA512 = 64
|
||||
} pika_hashlib_t;
|
||||
|
||||
void to_hex(uint8_t* s, int l, uint8_t* d);
|
||||
|
||||
void _hashlib_Hash_md5(PikaObj* self, Arg* data) {
|
||||
size_t size = arg_getBytesSize(data);
|
||||
uint8_t* data_h = arg_getBytes(data);
|
||||
|
||||
mbedtls_md5_context context;
|
||||
mbedtls_md5_init(&context);
|
||||
mbedtls_md5_starts(&context);
|
||||
if (size != 0) {
|
||||
mbedtls_md5_update(&context, data_h, size);
|
||||
}
|
||||
obj_setStruct(self, "context", context);
|
||||
obj_setInt(self, "mode", PIKA_HASHLIB_MD5);
|
||||
return;
|
||||
}
|
||||
|
||||
void _hashlib_Hash_sha1(PikaObj* self, Arg* data) {
|
||||
size_t size = arg_getBytesSize(data);
|
||||
uint8_t* data_h = arg_getBytes(data);
|
||||
|
||||
mbedtls_sha1_context context;
|
||||
mbedtls_sha1_init(&context);
|
||||
mbedtls_sha1_starts(&context);
|
||||
if (size != 0) {
|
||||
mbedtls_sha1_update(&context, data_h, size);
|
||||
}
|
||||
obj_setStruct(self, "context", context);
|
||||
obj_setInt(self, "mode", PIKA_HASHLIB_SHA1);
|
||||
return;
|
||||
}
|
||||
|
||||
void _hashlib_Hash_sha224(PikaObj* self, Arg* data) {
|
||||
size_t size = arg_getBytesSize(data);
|
||||
uint8_t* data_h = arg_getBytes(data);
|
||||
|
||||
mbedtls_sha256_context context;
|
||||
mbedtls_sha256_init(&context);
|
||||
mbedtls_sha256_starts(&context, 1);
|
||||
if (size != 0) {
|
||||
mbedtls_sha256_update(&context, data_h, size);
|
||||
}
|
||||
obj_setStruct(self, "context", context);
|
||||
obj_setInt(self, "mode", PIKA_HASHLIB_SHA256);
|
||||
return;
|
||||
}
|
||||
|
||||
void _hashlib_Hash_sha256(PikaObj* self, Arg* data) {
|
||||
size_t size = arg_getBytesSize(data);
|
||||
uint8_t* data_h = arg_getBytes(data);
|
||||
|
||||
mbedtls_sha256_context context;
|
||||
mbedtls_sha256_init(&context);
|
||||
mbedtls_sha256_starts(&context, 0);
|
||||
if (size != 0) {
|
||||
mbedtls_sha256_update(&context, data_h, size);
|
||||
}
|
||||
obj_setStruct(self, "context", context);
|
||||
obj_setInt(self, "mode", PIKA_HASHLIB_SHA256);
|
||||
return;
|
||||
}
|
||||
|
||||
void _hashlib_Hash_sha384(PikaObj* self, Arg* data) {
|
||||
size_t size = arg_getBytesSize(data);
|
||||
uint8_t* data_h = arg_getBytes(data);
|
||||
|
||||
mbedtls_sha512_context context;
|
||||
mbedtls_sha512_init(&context);
|
||||
mbedtls_sha512_starts(&context, 1);
|
||||
if (size != 0) {
|
||||
mbedtls_sha512_update(&context, data_h, size);
|
||||
}
|
||||
obj_setStruct(self, "context", context);
|
||||
obj_setInt(self, "mode", PIKA_HASHLIB_SHA512);
|
||||
return;
|
||||
}
|
||||
|
||||
void _hashlib_Hash_sha512(PikaObj* self, Arg* data) {
|
||||
size_t size = arg_getBytesSize(data);
|
||||
uint8_t* data_h = arg_getBytes(data);
|
||||
|
||||
mbedtls_sha512_context context;
|
||||
mbedtls_sha512_init(&context);
|
||||
mbedtls_sha512_starts(&context, 0);
|
||||
if (size != 0) {
|
||||
mbedtls_sha512_update(&context, data_h, size);
|
||||
}
|
||||
obj_setStruct(self, "context", context);
|
||||
obj_setInt(self, "mode", PIKA_HASHLIB_SHA512);
|
||||
return;
|
||||
}
|
||||
|
||||
void _hashlib_Hash_new(PikaObj* self, char* mode) {
|
||||
if (strcmp(mode, "md5") || strcmp(mode, "MD5")) {
|
||||
mbedtls_md5_context context;
|
||||
mbedtls_md5_init(&context);
|
||||
mbedtls_md5_starts(&context);
|
||||
obj_setStruct(self, "context", context);
|
||||
obj_setInt(self, "mode", PIKA_HASHLIB_MD5);
|
||||
} else if (strcmp(mode, "sha1") || strcmp(mode, "SHA1")) {
|
||||
mbedtls_sha1_context context;
|
||||
mbedtls_sha1_init(&context);
|
||||
mbedtls_sha1_starts(&context);
|
||||
obj_setStruct(self, "context", context);
|
||||
obj_setInt(self, "mode", PIKA_HASHLIB_SHA1);
|
||||
} else if (strcmp(mode, "sha1") || strcmp(mode, "SHA1")) {
|
||||
mbedtls_sha1_context context;
|
||||
mbedtls_sha1_init(&context);
|
||||
mbedtls_sha1_starts(&context);
|
||||
obj_setStruct(self, "context", context);
|
||||
obj_setInt(self, "mode", PIKA_HASHLIB_SHA1);
|
||||
} else if (strcmp(mode, "sha224") || strcmp(mode, "sha224")) {
|
||||
mbedtls_sha256_context context;
|
||||
mbedtls_sha256_init(&context);
|
||||
mbedtls_sha256_starts(&context, 1);
|
||||
obj_setStruct(self, "context", context);
|
||||
obj_setInt(self, "mode", PIKA_HASHLIB_SHA256);
|
||||
} else if (strcmp(mode, "sha256") || strcmp(mode, "SHA224")) {
|
||||
mbedtls_sha256_context context;
|
||||
mbedtls_sha256_init(&context);
|
||||
mbedtls_sha256_starts(&context, 0);
|
||||
obj_setStruct(self, "context", context);
|
||||
obj_setInt(self, "mode", PIKA_HASHLIB_SHA256);
|
||||
} else if (strcmp(mode, "sha384") || strcmp(mode, "SHA384")) {
|
||||
mbedtls_sha512_context context;
|
||||
mbedtls_sha512_init(&context);
|
||||
mbedtls_sha512_starts(&context, 1);
|
||||
obj_setStruct(self, "context", context);
|
||||
obj_setInt(self, "mode", PIKA_HASHLIB_SHA384);
|
||||
} else if (strcmp(mode, "sha512") || strcmp(mode, "SHA512")) {
|
||||
mbedtls_sha512_context context;
|
||||
mbedtls_sha512_init(&context);
|
||||
mbedtls_sha512_starts(&context, 0);
|
||||
obj_setStruct(self, "context", context);
|
||||
obj_setInt(self, "mode", PIKA_HASHLIB_SHA512);
|
||||
} else {
|
||||
obj_setErrorCode(self, -1); // not support mode
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void _hashlib_Hash_update(PikaObj* self, Arg* data) {
|
||||
size_t size = arg_getBytesSize(data);
|
||||
uint8_t* data_h = arg_getBytes(data);
|
||||
|
||||
if (size != 0) {
|
||||
void* context = obj_getStruct(self, "context");
|
||||
switch (obj_getInt(self, "mode")) {
|
||||
case PIKA_HASHLIB_MD5:
|
||||
mbedtls_md5_update((mbedtls_md5_context*)context, data_h, size);
|
||||
break;
|
||||
case PIKA_HASHLIB_SHA1:
|
||||
mbedtls_sha1_update((mbedtls_sha1_context*)context, data_h,
|
||||
size);
|
||||
break;
|
||||
case PIKA_HASHLIB_SHA224:
|
||||
case PIKA_HASHLIB_SHA256:
|
||||
mbedtls_sha256_update((mbedtls_sha256_context*)context, data_h,
|
||||
size);
|
||||
break;
|
||||
case PIKA_HASHLIB_SHA384:
|
||||
case PIKA_HASHLIB_SHA512:
|
||||
mbedtls_sha512_update((mbedtls_sha512_context*)context, data_h,
|
||||
size);
|
||||
break;
|
||||
default:
|
||||
obj_setErrorCode(self, -1); // not support mode
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
obj_setErrorCode(self, -2); // io error
|
||||
}
|
||||
}
|
||||
|
||||
Arg* _hashlib_Hash_digest(PikaObj* self) {
|
||||
obj_setBytes(self, "buff", NULL, 64);
|
||||
uint8_t* buff = obj_getBytes(self, "buff");
|
||||
void* context = obj_getStruct(self, "context");
|
||||
|
||||
switch (obj_getInt(self, "mode")) {
|
||||
case PIKA_HASHLIB_MD5:
|
||||
mbedtls_md5_finish((mbedtls_md5_context*)context, buff);
|
||||
return arg_newBytes(buff, PIKA_HASHLIB_MD5);
|
||||
break;
|
||||
case PIKA_HASHLIB_SHA1:
|
||||
mbedtls_sha1_finish((mbedtls_sha1_context*)context, buff);
|
||||
return arg_newBytes(buff, PIKA_HASHLIB_SHA1);
|
||||
break;
|
||||
case PIKA_HASHLIB_SHA224:
|
||||
mbedtls_sha256_finish((mbedtls_sha256_context*)context, buff);
|
||||
return arg_newBytes(buff, PIKA_HASHLIB_SHA224);
|
||||
break;
|
||||
case PIKA_HASHLIB_SHA256:
|
||||
mbedtls_sha256_finish((mbedtls_sha256_context*)context, buff);
|
||||
return arg_newBytes(buff, PIKA_HASHLIB_SHA256);
|
||||
break;
|
||||
case PIKA_HASHLIB_SHA384:
|
||||
mbedtls_sha512_finish((mbedtls_sha512_context*)context, buff);
|
||||
return arg_newBytes(buff, PIKA_HASHLIB_SHA384);
|
||||
break;
|
||||
case PIKA_HASHLIB_SHA512:
|
||||
mbedtls_sha512_finish((mbedtls_sha512_context*)context, buff);
|
||||
return arg_newBytes(buff, PIKA_HASHLIB_SHA512);
|
||||
break;
|
||||
default:
|
||||
obj_setErrorCode(self, -1); // not support mode
|
||||
return arg_newNull();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
char* _hashlib_Hash_hexdigest(PikaObj* self) {
|
||||
obj_setBytes(self, "hexbuff", NULL, 128);
|
||||
uint8_t* hexbuff = obj_getBytes(self, "hexbuff");
|
||||
uint8_t* buff = obj_getBytes(self, "buff");
|
||||
|
||||
if (buff == NULL) {
|
||||
obj_setBytes(self, "buff", NULL, 64);
|
||||
uint8_t* buff = obj_getBytes(self, "buff");
|
||||
void* context = obj_getStruct(self, "context");
|
||||
|
||||
switch (obj_getInt(self, "mode")) {
|
||||
case PIKA_HASHLIB_MD5:
|
||||
mbedtls_md5_finish((mbedtls_md5_context*)context, buff);
|
||||
break;
|
||||
case PIKA_HASHLIB_SHA1:
|
||||
mbedtls_sha1_finish((mbedtls_sha1_context*)context, buff);
|
||||
break;
|
||||
case PIKA_HASHLIB_SHA224:
|
||||
mbedtls_sha256_finish((mbedtls_sha256_context*)context, buff);
|
||||
break;
|
||||
case PIKA_HASHLIB_SHA256:
|
||||
mbedtls_sha256_finish((mbedtls_sha256_context*)context, buff);
|
||||
break;
|
||||
case PIKA_HASHLIB_SHA384:
|
||||
mbedtls_sha512_finish((mbedtls_sha512_context*)context, buff);
|
||||
break;
|
||||
case PIKA_HASHLIB_SHA512:
|
||||
mbedtls_sha512_finish((mbedtls_sha512_context*)context, buff);
|
||||
break;
|
||||
default:
|
||||
obj_setErrorCode(self, -1); // not support mode
|
||||
break;
|
||||
}
|
||||
to_hex(buff, obj_getInt(self, "mode"), hexbuff);
|
||||
} else {
|
||||
to_hex(buff, obj_getInt(self, "mode"), hexbuff);
|
||||
}
|
||||
return (char*)hexbuff;
|
||||
}
|
||||
|
||||
void _hashlib_Hash_copy(PikaObj* self) {}
|
||||
|
||||
const uint8_t hex_table[] = {'0', '1', '2', '3', '4', '5', '6', '7',
|
||||
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
|
||||
void to_hex(uint8_t* s, int l, uint8_t* d) {
|
||||
while (l--) {
|
||||
*(d++) = hex_table[*s >> 4];
|
||||
*(d++) = hex_table[*(s++) & 0x0f];
|
||||
}
|
||||
}
|
19
test/hashlib-test.cpp
Normal file
19
test/hashlib-test.cpp
Normal file
@ -0,0 +1,19 @@
|
||||
#include "test_common.h"
|
||||
|
||||
TEST(hashlib, new_) {
|
||||
/* init */
|
||||
pikaMemInfo.heapUsedMax = 0;
|
||||
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
|
||||
extern unsigned char pikaModules_py_a[];
|
||||
obj_linkLibrary(pikaMain, pikaModules_py_a);
|
||||
/* run */
|
||||
__platform_printf("BEGIN\r\n");
|
||||
obj_run(pikaMain,
|
||||
"import hashlib\r\n"
|
||||
"md5 = hashlib.new('md5')\r\n");
|
||||
/* collect */
|
||||
/* assert */
|
||||
/* deinit */
|
||||
obj_deinit(pikaMain);
|
||||
EXPECT_EQ(pikaMemNow(), 0);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user