Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将Thrift对象转换为SHA1摘要_Python_Hashlib - Fatal编程技术网

Python 将Thrift对象转换为SHA1摘要

Python 将Thrift对象转换为SHA1摘要,python,hashlib,Python,Hashlib,我是Python的新手。我尝试使用Thrift协议处理服务器 struct AuthSalt { 1: required i64 client, /* random data */ 2: required i64 server, /* data from previous answer */ } struct AuthRequest { 1: required AuthSalt bootstrap, 2: required string who,

我是Python的新手。我尝试使用Thrift协议处理服务器

struct AuthSalt {
    1: required i64 client,   /* random data */
    2: required i64 server,   /* data from previous answer */
}

struct AuthRequest {
    1: required AuthSalt bootstrap,
    2: required string who,           /* login */
    3: required string signature,     /* SHA-1: bootstrap + password + who + bootstrap. */
}

exception NotAuthorisedException {
    1: required string description
}

service Bookworm {
    AuthResponse Authenticate( 1: required AuthRequest a, 2: required string locale )
        throws ( 1: NotAuthorisedException e )
}
我需要使用以下算法创建SHA1摘要:引导+密码+谁+引导

要创建引导,我使用以下方法:

dig = hashlib.sha1
bootstrap = AuthSalt(0, 0)
dig.update(bootstrap)
dig.update(password + who)
dig.update(bootstrap)
但是,update方法参数只输入字符串,我不知道如何将引导转换为字符串

在C++中,这个代码看起来像:

SHA_CTX c;
            ::SHA1_Init(&c);
            ::SHA1_Update(&c, &bootstrap, sizeof(bootstrap));
            ::SHA1_Update(&c, password.c_str(), password.size());
            ::SHA1_Update(&c, who.c_str(), who.size());
            ::SHA1_Update(&c, &bootstrap, sizeof(bootstrap));
            ::SHA1_Final(digest, &c);
有人能解释一下如何使用python吗


提前谢谢

我认为
str(bootstrap)
而不是
bootstrap
应该可以工作。

我认为
str(bootstrap)
而不是
bootstrap
应该可以工作。

这就是我需要的:

for x in tuple(struct.pack("Q",bootstrap.client)):
  dig.update(x)

将i64转换为8个字节并用每个字节更新哈希值这就是我需要的:

for x in tuple(struct.pack("Q",bootstrap.client)):
  dig.update(x)

将i64转换为8个字节并用每个字节更新哈希值

否,这是不正确的。请仔细看C++代码。正如我所理解的str(bootstrap)-这是对象的字符串表示,但我需要bootstrap中两个字节的字符串表示。引导是一种结构(64位有符号整数)。str(bootstrap.client)+str(bootstrap.server)-也不正确。不,这不正确。请仔细看C++代码。正如我所理解的str(bootstrap)-这是对象的字符串表示,但我需要bootstrap中两个字节的字符串表示。引导是一种结构(64位有符号整数)。str(bootstrap.client)+str(bootstrap.server)-也不正确。