Python
將c++函式翻譯成python:sha256 hash mismatch
這與比特幣無關,但它使用了大部分比特幣程式碼庫。我想將一些 c++ 程式碼翻譯成 python,但很難得到正確的雜湊值。
這是我嘗試獲取的功能。雜湊正在做雙重 sha256
uint256 CalcKernelHash(const uint256& stakeModifier, int64_t height, int64_t coinstakeTime, const uint256& masternodeID, const Consensus::Params& params) { // Calculate hash CDataStream ss(SER_GETHASH, 0); ss << stakeModifier << coinstakeTime << GetMnCollateralAmount(int(height)) << masternodeID; std::cout << "\n--------------------------------\n"; std::cout << "DEBUG[" << coinstakeTime << "] collateral: " << GetMnCollateralAmount(int(height)) << "\n"; std::cout << "DEBUG[" << coinstakeTime << "] ss: " << stakeModifier.ToString() << coinstakeTime << GetMnCollateralAmount(int(height)) << masternodeID.ToString() << "\n"; std::cout << "DEBUG[" << coinstakeTime << "] HASH: " << Hash(ss.begin(), ss.end()).ToString() << "\n"; return Hash(ss.begin(), ss.end()); }
在這裡它被稱為
const auto hashProofOfStake = UintToArith256(CalcKernelHash(stakeModifier, height, coinstakeTime, masternodeID, params)); std::cout << "DEBUG[" << coinstakeTime << "] coinstakeTime: " << coinstakeTime << "\n"; std::cout << "DEBUG[" << coinstakeTime << "] nbits: " << nBits << "\n"; std::cout << "DEBUG[" << coinstakeTime << "] hashProofOfStake: " << hashProofOfStake.ToString() << "\n"; std::cout << "DEBUG[" << coinstakeTime << "] stakeModifier: " << stakeModifier.ToString() << "\n"; std::cout << "DEBUG[" << coinstakeTime << "] masternodeId: " << masternodeID.ToString() << "\n";
我添加了一些調試語句來檢查輸出並提供參考
-------------------------------- DEBUG[1622528110] collateral: 2000000000000 DEBUG[1622528110] ss: f541f372005beeb8952f964ea638527341c3b3a6eebb0c0309e4f092bd6c1b0e16225281102000000000000e2cfe3f576da4999ea2268224f34efdd3b0d567841d561b41b3ab67976c977d1 DEBUG[1622528110] HASH: e4b0d1b01a99d00e44d41eeb37c81d19940bf71270994fff53ea5caa1c79f663 DEBUG[1622528110] coinstakeTime: 1622528110 DEBUG[1622528110] nbits: 419654358 DEBUG[1622528110] hashProofOfStake: e4b0d1b01a99d00e44d41eeb37c81d19940bf71270994fff53ea5caa1c79f663 DEBUG[1622528110] stakeModifier: f541f372005beeb8952f964ea638527341c3b3a6eebb0c0309e4f092bd6c1b0e DEBUG[1622528110] masternodeId: e2cfe3f576da4999ea2268224f34efdd3b0d567841d561b41b3ab67976c977d1
這是我在 python 中的試用
import binascii import hashlib def rotate(x): s2 = bytearray.fromhex(x) s2.reverse() return binascii.hexlify(s2).decode() def padhexa(s): return s[2:].zfill(16) def CalcKernelHash(stakeModifier, coinstakeTime, masternodeId): stakeModifier = binascii.unhexlify(stakeModifier) coinstakeTime = binascii.unhexlify("%x" % coinstakeTime) collateral = binascii.unhexlify("0%x" % 2000000000000) masternodeId = binascii.unhexlify(masternodeId) bin_s = stakeModifier[::-1] + coinstakeTime[::-1] + collateral[::-1] + masternodeId[::1] print(bin_s) return rotate(hashlib.sha256(hashlib.sha256(bin_s).digest()).hexdigest()) CalcKernelHash('f541f372005beeb8952f964ea638527341c3b3a6eebb0c0309e4f092bd6c1b0e', 1622528110, 'e2cfe3f576da4999ea2268224f34efdd3b0d567841d561b41b3ab67976c977d1')
當我使用調試輸出中的參數執行時,我得到
CalcKernelHash('f541f372005beeb8952f964ea638527341c3b3a6eebb0c0309e4f092bd6c1b0e', 1622528110, 'e2cfe3f576da4999ea2268224f34efdd3b0d567841d561b41b3ab67976c977d1') 'f01ba34ac7a897a1e37d61bac3a8ea6f0126c2ae883a2d75dab1f407d40aad0b'
我應該去哪裡
hashProofOfStake e4b0d1b01a99d00e44d41eeb37c81d19940bf71270994fff53ea5caa1c79f663
編輯:
bin_s 返回
b'\x0e\x1bl\xbd\x92\xf0\xe4\t\x03\x0c\xbb\xee\xa6\xb3\xc3AsR8\xa6N\x96/\x95\xb8\xee[\x00r\xf3A\xf5n\xd0\xb5
\x00 J\xa9\xd1\x01\xe2\xcf\xe3\xf5v\xdaI\x99\xea"h"O4\xef\xdd;\rVxA\xd5a\xb4\x1b:\xb6yv\xc9w\xd1'`編輯2:
雜湊定義
/** Compute the 256-bit hash of an object. */ template<typename T1> inline uint256 Hash(const T1 pbegin, const T1 pend) { static const unsigned char pblank[1] = {}; uint256 result; CHash256().Write(pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0])) .Finalize((unsigned char*)&result); return result; }
def CalcKernelHash(stakeModifier, coinstakeTime, masternodeId): stakeModifier = binascii.unhexlify(stakeModifier) coinstakeTime = binascii.unhexlify(hex(coinstakeTime)[2:]) collateral = binascii.unhexlify("0000000000204AA9D1010000") masternodeId = binascii.unhexlify(masternodeId) bin_s = stakeModifier[::-1] + coinstakeTime[::-1] + collateral[::1] + masternodeId[::-1] print(binascii.hexlify(bin_s)) return rotate(hashlib.sha256(hashlib.sha256(bin_s).digest()).hexdigest())