首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用ctypes模块将blake散列函数C实现封装到Python中,还包括简单的python ctypes testvector脚本

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

Stack Overflow用户
提问于 2012-04-19 02:26:38
回答 1查看 417关注 0票数 0

我正在尝试使用C中的python ctypes来创建简单的库blake散列函数包装器,但只是为了测试一开始我的简单C助手函数是否能正确工作,我编写了一个小的python脚本blake散列函数测试向量。我的问题出现在这个小脚本上。我偶然发现了几个小时来解决这个问题。通过使用这个小辅助测试向量脚本,我总是得到意外的64字节输出值"blake 512 usage“。我意识到,我的问题来自于我写的c helper函数,当我试图返回64字节的数量时(准确地说),稍后将通过使用这个小的python ctypes脚本测试向量来使用。

我使用的纯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 ctype调用。

代码语言:javascript
复制
#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脚本测试向量在这里,只需尝试这个小脚本,将输出重定向到文件的任何文本,稍后就会捕捉到意想不到的值。

代码语言:javascript
复制
from ctypes import *
from string import printable
from itertools import permutations
from random import *
d = CDLL('blake.dll') #edit here your own dll or .so library
d.bl.restype = c_char_p

print '[+] Simple Test-vectors Blake hash function\n'
s = SystemRandom()
for x in permutations(printable):
p = ''.join(map(str,x))
q = list(p)
s.shuffle(q)
r= d.bl(''.join(map(str,q)))
if ((len(r)*2) != 0x80):
    print '\n[-] Not persistent value of 64 bytes was detected : %d'% len(r) 
    w = r.encode('hex')
    print w, '-->', len(w) 
    print '\n'
elif ((len(r)*2) == 0x80):
    print '\n',len(r), '\n',r.encode('hex')
    print '\n'

因此,为了得到预期的输出值,我将使用这个小Python脚本测试向量对上面的辅助C函数进行任何更正,以便稍后调用,非常感谢!顺便说一句,上面是一个更新的代码。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-04-19 10:39:12

在函数BitSequence * bl(char *input)的末尾,返回值output是一个局部变量,该变量在函数退出后将不存在。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10215595

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档