In a C program, I can change the internal state of the SHA256 algo as follows:
SHA256_CTX c;SHA256_Init(&c);for (i =0; i<64; i++) SHA256_Update(&c, "$", 1);# 0x44332211 is just for examplec.h[0] = htole32(0x44332211);c.h[1] = htole32(0x44332211);c.h[2] = htole32(0x44332211);c.h[3] = htole32(0x44332211);c.h[4] = htole32(0x44332211);c.h[5] = htole32(0x44332211);c.h[6] = htole32(0x44332211);c.h[7] = htole32(0x44332211);
Python provides hashlib.sha256
. I want to ask if there is any way in which the internal state of sha256 algo can be updated in Python similar to what I did in C above ?
As a part of IEFT standard, I need to hash the body of a JSON request.
I have a reference code in JS that is known to produce the right hash, but now I'm stuck with matching the result in Python.
The JSON body:
body = {"description":"Test ticket"}
The JS code:
bodyHash = CryptoJS.SHA256(body).toString(CryptoJS.enc.Base64)
Please note it's not JSON.stringify(body)
! The "correct" hash (the one I'm looking to match):
TqXFCKZWbnYkBUP4/rBv1Fd3e+OVScQBZDav2mXSMw4=
The Python solution should be along the lines of:
body_hash = base64.b64encode(hashlib.sha256(repr(body).encode('utf-8')).digest()).decode('ascii')
Which produces something different:
z0RpYmKAIfVibpSLJx/iRDZaNHJkJ2+rcenc1KEEAUw=
Is there even a way to match those hashes? How the JS object is hashed in CryptoJS?
This specific case is coming whenever i tried to encrypt single char strings in application. When i tried to encrypt the same string with main method then i was able to encrypt it. But when i run with the application then this specific issue is coming. I felt this is strange because it is working with the main method.
My encryption code will be as follows.
public static String encryptWithAES256(String strToEncrypt) throws Exception{ MessageDigest digest = MessageDigest.getInstance("SHA-256"); byte[] encodedhash = digest.digest(KEY.getBytes(StandardCharsets.UTF_8)); IvParameterSpec ivspec = new IvParameterSpec(Arrays.copyOf(KEY.getBytes(),16)); SecretKeySpec secretKey = new SecretKeySpec(encodedhash, AES_ENCRYPTION_ALGORITHM); Cipher cipher = Cipher.getInstance(CIPHER_TRANSFORMATION); cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivspec); return new String(Base64.encodeBase64(cipher.doFinal(strToEncrypt.getBytes(CHARACTER_ENCODING))));}
I have a piece of code that converting email addresses to HASH using SHA256 algorithm. When running the code, the expected results are presented, but when i'm trying to output the results - i'm getting an error :ValueError: I/O operation on closed file
import osimport hashlibwith open("emails.txt", "r") as text: for line in text.readlines(): line = line.rstrip("\n") m = hashlib.sha256(line) print(m.hexdigest())with open("Output.txt", "w") as text_file: for row in text: print row text_file.write("%s\n" % str(row))
can anyone help me to get all the results to external file?
Suppose a user designed a new scheme to detect whether a message is modified by an attacker (protecting data integrity). The scheme is to append to each message a SHA-256 hash of that message. Will there be any vulnerability in the design?
Please note that by viewing our site you agree to our use of cookies (see Pribadi for details). You will only see this message once.