使用ctypes模块将blake散列函数C实现封装到Python中,还包括了简单的pythonctypes testvector脚本

2024-05-16 16:15:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用C语言中的python ctypes来制作简单的blake hash函数包装器,但只是为了测试我的简单C helper函数是否能正常工作,我编写了小python脚本blake hash函数测试向量。我的问题出现在这个小剧本上。我几个小时都在设法解决这个问题。通过使用这个小的helper测试向量脚本,我总是意外地得到64字节的输出值“blake 512 usage”。我意识到,我的问题来自于我编写的c helper函数,当我试图返回64字节的数量(应该是确切的)时,稍后将使用这个小python cypes脚本测试向量。在

我使用的纯C实现源直接从NIST Blake提交的优化32位处理器下载,文件名为Blake_opt32.C和Blake_opt32.h。可以在http://csrc.nist.gov/groups/ST/hash/sha-3/Round3/documents/Blake_FinalRnd.zip下载

下面是我的简单C助手函数,稍后将使用python ctypes调用它。在

#include <stdio.h>
#include "blake_opt32.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <stdint.h>

BitSequence msg[65];

size_t    strlcpy(unsigned char *dst, const char *src, size_t siz)
{
unsigned char *d = dst;
const char *s = src;
size_t n = siz;

/* Copy as many bytes as will fit */
if (n != 0) {
    while (--n != 0) {
        if ((*d++ = *s++) == '\0')
            break;
    }
}

/* Not enough room in dst, add NUL and traverse rest of src */
if (n == 0) {
    if (siz != 0)
        *d = '\0';      /* NUL-terminate dst */
    while (*s++)
        ;
}

return(s - src - 1);    /* count does not include NUL */
}

BitSequence * bl(char *input)
{
BitSequence output[65];
BitSequence msg[sizeof(output)];
int dInt;

memset(output,0,sizeof(output));
dInt = strlen(input);

if (dInt > 0xffff){
    exit( 1);
}
BitSequence data[dInt];

memset(data, 0, dInt);
strlcpy(data, input, sizeof(data));
DataLength dLen =1152;
Hash(512, data, dLen, output);
int x;
for (x=0;x<64;++x){
    printf("%02X",output[x]);
}
memcpy(msg,output,sizeof(output));
//here the problem araised, when trying to return unsigned char or BitSequence value, the unexpected output of small python scipt test vectors value is detected 
return  msg; 
}  

简单的python脚本测试向量就在这里,只要尝试这个小脚本,将输出重定向到任何文本的文件中,之后意外的值就会被捕捉到。在

^{pr2}$

因此,以上任何修改我的helper C函数,以便以后使用这个小Python脚本测试向量来调用,以获得预期的输出值,在此之前非常感谢! 顺便说一下,这是一个更新的代码。在


Tags: 函数helper脚本outputdataifincludemsg