Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm doing some experiment with the following code:
import hashlib
password = 16
n = 2 #counter
hash = hashlib.sha256(str(password) + str(n)).hexdigest() #will read the password as string and hash
print hash
What should be the proper code so it can accept a 32byte hex like 000000...0002?
A hex in Python is just a fancy way of writing an integer. So simply set password to your hex value:
password = 0x00000000000000000000000000000002
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 days ago.
Improve this question
The whitespace should be replaced with a 'dollar' sign.
Keep the punctuation marks unchanged.
Note: Convert the given input sentence into lowercase before encrypting.
The final output should be lowercase.
input = Hello World
expected_output = svool$dliow
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 months ago.
Improve this question
I would like to know how to write in a single line the combination of two hash algorithms in Python. I want to use MD5 and sha256 in one line. Here is my function:
def calcHash(self):
block_string = json.dumps({"nonce":self.nonce, "tstamp":self.tstamp, "output":self.output, "prevhash":self.prevhash}, sort_keys=True).encode()
return hashlib.sha256(block_string).hexdigest()
I tried return md5.hashlib(hashlib.sha256(block_string).hexdigest()) but it doesn't work.
Can someone help?
The problem could just be that .hexdigest() produces a string, not a bytes, so you need to encode it again
>>> import hashlib, json
>>> j = json.dumps({"foo":"bar","baz":2022})
>>> hashlib.sha256(j.encode()).hexdigest()
'44ce0f46288befab7f32e9acd68d36b2a9997e801fb129422ddc35610323c627'
>>> hashlib.md5(hashlib.sha256(j.encode()).hexdigest().encode()).hexdigest()
'f7c6be48b7d34bcd278257dd73038f67'
Alternatively, you could directly use .digest() to get bytes from the hash, rather than intermediately converting it to a hex string (though note the output is different)
>>> hashlib.md5(hashlib.sha256(j.encode()).digest()).hexdigest()
'12ac82f5a1c0cb3751619f6d0ae0c2ee'
If you have a great many algorithms in some order, you may find .new() useful to iteratively produce a result from some list of hashes.
BEWARE
md5 is broken from a security perspective and I cannot recommend using it unless it's a required input to a legacy system!
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I was given some code in groovy and asked to convert it to Python.
def generateHmac(String data){
String secretKey = '123445667777'
byte[] digest
Mac mac = Mac.getInstance("HmacSHA256")
SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(), "HmacSHA256")
mac.init(secretKeySpec)
digest = mac.doFinal(data.getBytes())
//we use hex encoding for our tokens.
String token = digest.encodeHex().toString()
return token
}
I have tried many times but failed:
def generateHmac(String data):
data = "my_url"
secret_key = b"123445667777"
h = SHA256.new()
h.update(secret_key)
h.update(b"data")
print(h.hexdigest())
An hmac is not the same thing as a digest. HmacSHA26 uses SHA256, it isn't the same thing as SHA256. You need to use the hmac library to get the correct result.
hmac.digest(secretKey, message, "Sha256")
should give you what you want, with the secretKey and message being bytes.
The above is equivalent to the longer form:
hmac.new(secretKey, message, "Sha256").digest()
but once you have the hmac object, you can other things like update() with more text or hexdigest() to get a text-like digest.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
How can I get the number of codepoints in a string which may contain unicode characters 3 byte long. https://unicode-table.com/
For example for "I❤U" I would like to get 3.
Doing len(str) returns the number of bytes, so for the above example I would get 5.
Try to decode it in python2:
"I❤U".decode('utf-8')
Output: u'I\u2764U'
then len("I❤U".decode('utf-8')), it will be 3
In my env, I tried your code. But my result of len("I❤U") is 3.
>>> len("I❤U")
3
>>>
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Here is what I currently have, which is not working:
if "Forwarder" not in shp_name or "T_" not in shp_name or "Grad" not in shp_name:
I've also tried:
if ("Forwarder", "T_", "Grad") not in shp_name:
Samples of the input would be "DS_Forwarder_1" or "DS_Harvester_1". The script proceeds directly to else as it's unable to identify any of the above substrings in the primary string.
Try using the any built in.
if any(s in shp_name for s in ("Forwarder", "T_", "Grad")):
...
This will be true if any of the given strings are present in shp_name. You can use if not any(... if you want False when one of the strings is present.