pythonstringcastingbinary-string

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

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

 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


Solution

  • From the str docs

    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.

    So str(message) is roughly equivalent to message.__str__()[1]. What it does depends entirely on the class of message. So you need to figure out the type of that message argument and look up its documentation.


    [1] I say "roughly equivalent". As noted in the docs, it's actually equivalent to 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. Assuming you're using a relatively standard library, I doubt either of those is in play here.