2012-03-23 47 views
1

我是Python中的新手。我尝试使用节俭协议将Thrift对象转换为SHA1摘要

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) 
} 

我需要使用这个algoritm创建SHA1摘要与服务器一起工作:引导+密码+谁+引导。

要创建引导我用这个:

dig = hashlib.sha1 
bootstrap = AuthSalt(0, 0) 
dig.update(bootstrap) 
dig.update(password + who) 
dig.update(bootstrap) 

更新方法的参数类型只字符串,我不知道如何引导转换为字符串。

在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办呢?

在此先感谢!

回答

1

我想str(bootstrap)而不是bootstrap应该工作。

+0

不,这是不正确的。请仔细查看C++代码。据我了解str(bootstrap) - 这是对象的字符串表示,但我需要从bootstrap开始的两个字节的字符串表示。 Bootstrap是结构(64位有符号整数)。 str(bootstrap.client)+ str(bootstrap.server) - 也不正确。 – Ilya 2012-03-24 07:26:02

0

这是什么,我需要:

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

转换成I64 8个字节和更新哈希每字节