简体   繁体   English

强制转换为 str 解码按位字符串

[英]does casting to a str decode a bitwise string

observe the following观察以下

signature = rsa.sign(str(message), private_key, 'SHA-1')

see how message is being casti to a str看看消息是如何被转换到 str 的

would that decode a bit wise string?那会解码一个明智的字符串吗?

why I ask.为什么我问。

the code snippet above is being used by the aws boto library which is using the rsa libary and I keep getting an error from the rsa library上面的代码片段被使用 rsa 库的 aws boto 库使用,我不断从 rsa 库中收到错误

 File "/Users/bullshit/Documents/softwareprojects/shofi/backend/virtualshofi/lib/python3.8/site-packages/boto/cloudfront/distribution.py", line 677, in _sign_string
    signature = rsa.sign(str(message), private_key, 'SHA-1')
  File "/Users/bullshit/Documents/softwareprojects/shofi/backend/virtualshofi/lib/python3.8/site-packages/rsa/pkcs1.py", line 337, in sign
    msg_hash = compute_hash(message, hash_method)
  File "/Users/bullshit/Documents/softwareprojects/shofi/backend/virtualshofi/lib/python3.8/site-packages/rsa/pkcs1.py", line 439, in compute_hash
    assert hasattr(message, "read") and hasattr(message.read, "__call__")
AssertionError

The error is not important to the context of my question but its good for grounding.该错误对我的问题的上下文并不重要,但它有利于接地。 The reason why I am asking is also out of context我问的原因也是断章取义

Thank you谢谢

From the str docsstr文档

If neither encoding nor errors is given, str(object) returns type(object).__str__(object) , which is the “informal” or nicely printable string representation of object .如果既没有给出编码也没有给出错误,则str(object)返回type(object).__str__(object) ,这是object的“非正式”或可很好打印的字符串表示形式。

So str(message) is roughly equivalent to message.__str__() [1] .所以str(message)大致相当于message.__str__() [1] What it does depends entirely on the class of message .它的作用完全取决于message的类别。 So you need to figure out the type of that message argument and look up its documentation.因此,您需要弄清楚该消息参数的类型并查找其文档。


[1] I say "roughly equivalent". [1]我说“大致相等”。 As noted in the docs, it's actually equivalent to type(object).__str__(object) .如文档中所述,它实际上等同于type(object).__str__(object) The difference is usually trivial in the course of normal Python programming, but it can make a difference if a function is assigned directly to an instance or if some object with a funny __get__ is declared inside the class.在普通的 Python 编程过程中,这种差异通常是微不足道的,但是如果将函数直接分配给实例,或者在类中声明了带有有趣__get__的对象,则差异可能会有所不同。 Assuming you're using a relatively standard library, I doubt either of those is in play here.假设您使用的是相对标准的库,我怀疑其中任何一个都在这里发挥作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM