本文整理汇总了Java中nxt.crypto.Crypto.sha256方法的典型用法代码示例。如果您正苦于以下问题:Java Crypto.sha256方法的具体用法?Java Crypto.sha256怎么用?Java Crypto.sha256使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类nxt.crypto.Crypto
的用法示例。
在下文中一共展示了Crypto.sha256方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: processRequest
import nxt.crypto.Crypto; //导入方法依赖的package包/类
@Override
JSONStreamAware processRequest(HttpServletRequest req) {
String unsignedBytesString = Convert.emptyToNull(req.getParameter("unsignedTransactionBytes"));
String signatureHashString = Convert.emptyToNull(req.getParameter("signatureHash"));
if (unsignedBytesString == null) {
return MISSING_UNSIGNED_BYTES;
} else if (signatureHashString == null) {
return MISSING_SIGNATURE_HASH;
}
MessageDigest digest = Crypto.sha256();
digest.update(Convert.parseHexString(unsignedBytesString));
byte[] fullHash = digest.digest(Convert.parseHexString(signatureHashString));
JSONObject response = new JSONObject();
response.put("fullHash", Convert.toHexString(fullHash));
return response;
}
示例2: BlockNXTImpl
import nxt.crypto.Crypto; //导入方法依赖的package包/类
BlockNXTImpl(int timestamp, Block previousBlock1, byte[] publicKey, List<TransactionImpl> transactions) throws NxtException.ValidationException {
/* super(version, timestamp, previousBlockId, totalAmountNQT, totalFeeNQT, payloadLength, payloadHash,
previousBlockHash, List<TransactionImpl> transactions); */
super(timestamp, previousBlock1, transactions);
BlockNXTImpl previousBlock = (BlockNXTImpl) previousBlock1;
MessageDigest digest = Crypto.sha256();
digest.update(previousBlock.getGenerationSignature());
byte[] generationSignature = digest.digest(publicKey);
byte[] previousBlockHash = previousBlock.getHash();
this.version = 3;
this.generatorPublicKey = publicKey;
this.generationSignature = generationSignature;
this.blockSignature = blockSignature;
}
示例3: processRequest
import nxt.crypto.Crypto; //导入方法依赖的package包/类
@Override
JSONStreamAware processRequest(HttpServletRequest req) throws ParameterException {
String unsignedBytesString = Convert.emptyToNull(req.getParameter("unsignedTransactionBytes"));
String signatureHashString = Convert.emptyToNull(req.getParameter("signatureHash"));
String unsignedTransactionJSONString = Convert.emptyToNull(req.getParameter("unsignedTransactionJSON"));
if (signatureHashString == null) {
return MISSING_SIGNATURE_HASH;
}
JSONObject response = new JSONObject();
try {
Transaction transaction = ParameterParser.parseTransaction(unsignedTransactionJSONString, unsignedBytesString, null).build();
MessageDigest digest = Crypto.sha256();
digest.update(transaction.getUnsignedBytes());
byte[] fullHash = digest.digest(Convert.parseHexString(signatureHashString));
response.put("fullHash", Convert.toHexString(fullHash));
} catch (NxtException.NotValidException e) {
JSONData.putException(response, e, "Incorrect unsigned transaction json or bytes");
}
return response;
}
示例4: getHash
import nxt.crypto.Crypto; //导入方法依赖的package包/类
@Override
public byte[] getHash() {
if (data == null) {
return null;
}
MessageDigest digest = Crypto.sha256();
digest.update(Convert.toBytes(name));
digest.update(Convert.toBytes(description));
digest.update(Convert.toBytes(tags));
digest.update(Convert.toBytes(type));
digest.update(Convert.toBytes(channel));
digest.update((byte)(isText ? 1 : 0));
digest.update(Convert.toBytes(filename));
digest.update(data);
return digest.digest();
}
示例5: verifyChecksum
import nxt.crypto.Crypto; //导入方法依赖的package包/类
private boolean verifyChecksum(byte[] validChecksum) {
MessageDigest digest = Crypto.sha256();
try (Connection con = Db.db.getConnection();
PreparedStatement pstmt = con.prepareStatement(
"SELECT * FROM transaction ORDER BY id ASC, timestamp ASC");
DbIterator<TransactionImpl> iterator = blockchain.getTransactions(con, pstmt)) {
while (iterator.hasNext()) {
digest.update(iterator.next().getBytes());
}
} catch (SQLException e) {
throw new RuntimeException(e.toString(), e);
}
byte[] checksum = digest.digest();
if (validChecksum == null) {
Logger.logMessage("Checksum calculated:\n" + Arrays.toString(checksum));
return true;
} else if (!Arrays.equals(checksum, validChecksum)) {
Logger.logErrorMessage("Checksum failed at block " + blockchain.getHeight() + ": " + Arrays.toString(checksum));
return false;
} else {
Logger.logMessage("Checksum passed at block " + blockchain.getHeight());
return true;
}
}
示例6: addGenesisBlock
import nxt.crypto.Crypto; //导入方法依赖的package包/类
private void addGenesisBlock() {
if (BlockDb.hasBlock(Genesis.GENESIS_BLOCK_ID)) {
Logger.logMessage("Genesis block already in database");
BlockImpl lastBlock = BlockDb.findLastBlock();
blockchain.setLastBlock(lastBlock);
Logger.logMessage("Last block height: " + lastBlock.getHeight());
return;
}
Logger.logMessage("Genesis block not in database, starting from scratch");
try {
List<TransactionImpl> transactions = new ArrayList<>();
MessageDigest digest = Crypto.sha256();
for (Transaction transaction : transactions) {
digest.update(transaction.getBytes());
}
ByteBuffer bf = ByteBuffer.allocate( 0 );
bf.order( ByteOrder.LITTLE_ENDIAN );
byte[] byteATs = bf.array();
BlockImpl genesisBlock = new BlockImpl(-1, 0, 0, 0, 0, transactions.size() * 128, digest.digest(),
Genesis.CREATOR_PUBLIC_KEY, new byte[32], Genesis.GENESIS_BLOCK_SIGNATURE, null, transactions, 0, byteATs);
genesisBlock.setPrevious(null);
addBlock(genesisBlock);
} catch (NxtException.ValidationException e) {
Logger.logMessage(e.getMessage());
throw new RuntimeException(e.toString(), e);
}
}
示例7: getHit
import nxt.crypto.Crypto; //导入方法依赖的package包/类
static BigInteger getHit(byte[] publicKey, Block block1) {
BlockNXTImpl block = (BlockNXTImpl) block1;
/*if (allowsFakeForging(publicKey)) {
return BigInteger.ZERO;
}*/
if (block.getHeight() < Constants.TRANSPARENT_FORGING_BLOCK) {
throw new IllegalArgumentException("Not supported below Transparent Forging Block");
}
MessageDigest digest = Crypto.sha256();
digest.update(block.getGenerationSignature());
byte[] generationSignatureHash = digest.digest(publicKey);
return new BigInteger(1, new byte[] {generationSignatureHash[7], generationSignatureHash[6], generationSignatureHash[5], generationSignatureHash[4], generationSignatureHash[3], generationSignatureHash[2], generationSignatureHash[1], generationSignatureHash[0]});
}
示例8: verifyChecksum
import nxt.crypto.Crypto; //导入方法依赖的package包/类
private boolean verifyChecksum(byte[] validChecksum, int fromHeight, int toHeight) {
MessageDigest digest = Crypto.sha256();
try (Connection con = Db.db.getConnection();
PreparedStatement pstmt = con.prepareStatement(
"SELECT * FROM transaction WHERE height >= ? AND height <= ? ORDER BY id ASC, timestamp ASC")) {
pstmt.setInt(1, fromHeight);
pstmt.setInt(2, toHeight);
try (DbIterator<TransactionImpl> iterator = blockchain.getTransactions(con, pstmt)) {
while (iterator.hasNext()) {
digest.update(iterator.next().bytes());
}
}
} catch (SQLException e) {
throw new RuntimeException(e.toString(), e);
}
byte[] checksum = digest.digest();
if (validChecksum == null) {
Logger.logMessage("Checksum calculated:\n" + Arrays.toString(checksum));
return true;
} else if (!Arrays.equals(checksum, validChecksum)) {
Logger.logErrorMessage("Checksum failed at block " + blockchain.getHeight() + ": " + Arrays.toString(checksum));
return false;
} else {
Logger.logMessage("Checksum passed at block " + blockchain.getHeight());
return true;
}
}
示例9: getHit
import nxt.crypto.Crypto; //导入方法依赖的package包/类
static BigInteger getHit(byte[] publicKey, Block block) {
if (allowsFakeForging(publicKey)) {
return BigInteger.ZERO;
}
if (block.getHeight() < Constants.TRANSPARENT_FORGING_BLOCK) {
throw new IllegalArgumentException("Not supported below Transparent Forging Block");
}
MessageDigest digest = Crypto.sha256();
digest.update(block.getGenerationSignature());
byte[] generationSignatureHash = digest.digest(publicKey);
return new BigInteger(1, new byte[] {generationSignatureHash[7], generationSignatureHash[6], generationSignatureHash[5], generationSignatureHash[4], generationSignatureHash[3], generationSignatureHash[2], generationSignatureHash[1], generationSignatureHash[0]});
}
示例10: getHash
import nxt.crypto.Crypto; //导入方法依赖的package包/类
@Override
public byte[] getHash() {
if (hash != null) {
return hash;
}
MessageDigest digest = Crypto.sha256();
digest.update((byte)(isText ? 1 : 0));
digest.update(message);
return digest.digest();
}
示例11: get_Random_Id_for_Tx_in_A
import nxt.crypto.Crypto; //导入方法依赖的package包/类
@Override
public long get_Random_Id_for_Tx_in_A( AT_Machine_State state ) {
long txId = AT_API_Helper.getLong( state.get_A1() );
Transaction tx = Nxt.getBlockchain().getTransaction( txId );
if ( tx != null && tx.getHeight() >= state.getHeight() )
{
tx = null;
}
if ( tx !=null )
{
int txBlockHeight = tx.getHeight();
int blockHeight = state.getHeight();
if ( blockHeight - txBlockHeight < AT_Constants.getInstance().BLOCKS_FOR_RANDOM( blockHeight ) ){ //for tests - for real case 1440
state.setWaitForNumberOfBlocks( (int)AT_Constants.getInstance().BLOCKS_FOR_RANDOM( blockHeight ) - ( blockHeight - txBlockHeight ) );
state.getMachineState().pc -= 7;
state.getMachineState().stopped = true;
return 0;
}
MessageDigest digest = Crypto.sha256();
byte[] senderPublicKey = tx.getSenderPublicKey();
ByteBuffer bf = ByteBuffer.allocate( 32 + Long.SIZE + senderPublicKey.length );
bf.order( ByteOrder.LITTLE_ENDIAN );
bf.put(Nxt.getBlockchain().getBlockAtHeight(blockHeight - 1).getGenerationSignature());
bf.putLong( tx.getId() );
bf.put( senderPublicKey);
digest.update(bf.array());
byte[] byteRandom = digest.digest();
long random = Math.abs( AT_API_Helper.getLong( Arrays.copyOfRange(byteRandom, 0, 8) ) );
//System.out.println( "info: random for txid: " + Convert.toUnsignedLong( tx.getId() ) + "is: " + random );
return random;
}
return -1;
}
示例12: BlockImpl
import nxt.crypto.Crypto; //导入方法依赖的package包/类
BlockImpl(int timestamp, Block previousBlock, List<TransactionImpl> transactions) throws NxtException.ValidationException {
this.timestamp = timestamp;
if (previousBlock != null){
this.previousBlockId = previousBlock.getId();
this.previousBlockHash = previousBlock.getHash();
this.height = previousBlock.getHeight() + 1;
}else{
this.previousBlockId = 0;
this.previousBlockHash = new byte[32];
this.height = 0;
}
//this.generatorPublicKey = generatorPublicKey;
//this.generationSignature = generationSignature;
//this.blockSignature = blockSignature;
if (transactions != null) {
this.blockTransactions = Collections.unmodifiableList(transactions);
if (blockTransactions.size() > Constants.MAX_NUMBER_OF_TRANSACTIONS) {
throw new NxtException.NotValidException("attempted to create a block with " + blockTransactions.size() + " transactions");
}
long totalAmountNQT = 0;
long totalFeeNQT = 0;
int payloadLength = 0;
MessageDigest digest = Crypto.sha256();
for(Transaction tx : transactions) {
totalAmountNQT += tx.getAmountNQT();
totalFeeNQT += tx.getFeeNQT();
payloadLength += tx.getSize();
digest.update(tx.getBytes());
}
byte[] payloadHash = digest.digest();
this.totalAmountNQT = totalAmountNQT;
this.totalFeeNQT = totalFeeNQT;
this.payloadLength = payloadLength;
this.payloadHash = payloadHash;
if (payloadLength > Constants.MAX_PAYLOAD_LENGTH || payloadLength < 0) {
throw new NxtException.NotValidException("attempted to create a block with payloadLength " + payloadLength);
}
/*
for(TransactionImpl tx : transactions) {
tx.setBlock(this); //cannot be here, because blockId is not yet finalized
}*/
}else{
throw new NullPointerException("blockTransactions");
}
}
示例13: verifyGenerationSignature
import nxt.crypto.Crypto; //导入方法依赖的package包/类
boolean verifyGenerationSignature() {
try {
BlockNXTImpl previousBlock = (BlockNXTImpl) BlockchainImpl.getInstance().getBlock(getPreviousBlockId());
if (previousBlock == null) {
throw new RuntimeException("Can't verify signature because previous block is missing");
}
if (version == 1 && !Crypto.verify(generationSignature, previousBlock.generationSignature, generatorPublicKey, version >= 3)) {
return false;
}
Account account = Account.getAccount(getGeneratorId());
long effectiveBalance = account == null ? 0 : account.getEffectiveBalanceNXT();
if (effectiveBalance <= 0) {
return false;
}
MessageDigest digest = Crypto.sha256();
byte[] generationSignatureHash;
if (version == 1) {
generationSignatureHash = digest.digest(generationSignature);
} else {
digest.update(previousBlock.generationSignature);
generationSignatureHash = digest.digest(generatorPublicKey);
if (!Arrays.equals(generationSignature, generationSignatureHash)) {
return false;
}
}
BigInteger hit = new BigInteger(1, new byte[]{generationSignatureHash[7], generationSignatureHash[6], generationSignatureHash[5], generationSignatureHash[4], generationSignatureHash[3], generationSignatureHash[2], generationSignatureHash[1], generationSignatureHash[0]});
return ProofOfNXT.verifyHit(hit, BigInteger.valueOf(effectiveBalance), previousBlock, timestamp);
} catch (RuntimeException e) {
Logger.logMessage("Error verifying block generation signature", e);
return false;
}
}
示例14: validateTransactions
import nxt.crypto.Crypto; //导入方法依赖的package包/类
private void validateTransactions(BlockImpl block, BlockImpl previousLastBlock, int curTime, Map<TransactionType, Map<String, Boolean>> duplicates) throws BlockNotAcceptedException {
long payloadLength = 0;
long calculatedTotalAmount = 0;
long calculatedTotalFee = 0;
MessageDigest digest = Crypto.sha256();
for (TransactionImpl transaction : block.getTransactions()) {
if (transaction.getTimestamp() > curTime + Constants.MAX_TIMEDRIFT) {
throw new BlockOutOfOrderException("Invalid transaction timestamp: " + transaction.getTimestamp()
+ ", current time is " + curTime, block);
}
// cfb: Block 303 contains a transaction which expired before the block timestamp
if (transaction.getTimestamp() > block.getTimestamp() + Constants.MAX_TIMEDRIFT
|| (transaction.getExpiration() < block.getTimestamp() && previousLastBlock.getHeight() != 303)) {
throw new TransactionNotAcceptedException("Invalid transaction timestamp " + transaction.getTimestamp()
+ ", current time is " + curTime + ", block timestamp is " + block.getTimestamp(), transaction);
}
if (TransactionDb.hasTransaction(transaction.getId(), previousLastBlock.getHeight())) {
throw new TransactionNotAcceptedException("Transaction is already in the blockchain", transaction);
}
if (transaction.referencedTransactionFullHash() != null) {
if ((previousLastBlock.getHeight() < Constants.REFERENCED_TRANSACTION_FULL_HASH_BLOCK
&& !TransactionDb.hasTransaction(Convert.fullHashToId(transaction.referencedTransactionFullHash()), previousLastBlock.getHeight()))
|| (previousLastBlock.getHeight() >= Constants.REFERENCED_TRANSACTION_FULL_HASH_BLOCK
&& !hasAllReferencedTransactions(transaction, transaction.getTimestamp(), 0))) {
throw new TransactionNotAcceptedException("Missing or invalid referenced transaction "
+ transaction.getReferencedTransactionFullHash(), transaction);
}
}
if (transaction.getVersion() != getTransactionVersion(previousLastBlock.getHeight())) {
throw new TransactionNotAcceptedException("Invalid transaction version " + transaction.getVersion()
+ " at height " + previousLastBlock.getHeight(), transaction);
}
if (!transaction.verifySignature()) {
throw new TransactionNotAcceptedException("Transaction signature verification failed at height " + previousLastBlock.getHeight(), transaction);
}
/*
if (!EconomicClustering.verifyFork(transaction)) {
Logger.logDebugMessage("Block " + block.getStringId() + " height " + (previousLastBlock.getHeight() + 1)
+ " contains transaction that was generated on a fork: "
+ transaction.getStringId() + " ecBlockHeight " + transaction.getECBlockHeight() + " ecBlockId "
+ Convert.toUnsignedLong(transaction.getECBlockId()));
//throw new TransactionNotAcceptedException("Transaction belongs to a different fork", transaction);
}
*/
if (transaction.getId() == 0L) {
throw new TransactionNotAcceptedException("Invalid transaction id 0", transaction);
}
try {
transaction.validate();
} catch (NxtException.ValidationException e) {
throw new TransactionNotAcceptedException(e.getMessage(), transaction);
}
if (transaction.getPhasing() == null && transaction.isDuplicate(duplicates)) {
throw new TransactionNotAcceptedException("Transaction is a duplicate", transaction);
}
calculatedTotalAmount += transaction.getAmountNQT();
calculatedTotalFee += transaction.getFeeNQT();
payloadLength += transaction.getFullSize();
digest.update(transaction.bytes());
}
if (calculatedTotalAmount != block.getTotalAmountNQT() || calculatedTotalFee != block.getTotalFeeNQT()) {
throw new BlockNotAcceptedException("Total amount or fee don't match transaction totals", block);
}
if (!Arrays.equals(digest.digest(), block.getPayloadHash())) {
throw new BlockNotAcceptedException("Payload hash doesn't match", block);
}
if (payloadLength > block.getPayloadLength()) {
throw new BlockNotAcceptedException("Transaction payload length " + payloadLength + " exceeds declared block payload length " + block.getPayloadLength(), block);
}
}
示例15: verifyGenerationSignature
import nxt.crypto.Crypto; //导入方法依赖的package包/类
boolean verifyGenerationSignature() throws BlockchainProcessor.BlockOutOfOrderException {
try {
BlockImpl previousBlock = BlockchainImpl.getInstance().getBlock(getPreviousBlockId());
if (previousBlock == null) {
throw new BlockchainProcessor.BlockOutOfOrderException("Can't verify signature because previous block is missing", this);
}
if (version == 1 && !Crypto.verify(generationSignature, previousBlock.generationSignature, getGeneratorPublicKey(), version >= 3)) {
return false;
}
Account account = Account.getAccount(getGeneratorId());
long effectiveBalance = account == null ? 0 : account.getEffectiveBalanceNXT();
if (effectiveBalance <= 0) {
return false;
}
MessageDigest digest = Crypto.sha256();
byte[] generationSignatureHash;
if (version == 1) {
generationSignatureHash = digest.digest(generationSignature);
} else {
digest.update(previousBlock.generationSignature);
generationSignatureHash = digest.digest(getGeneratorPublicKey());
if (!Arrays.equals(generationSignature, generationSignatureHash)) {
return false;
}
}
BigInteger hit = new BigInteger(1, new byte[]{generationSignatureHash[7], generationSignatureHash[6], generationSignatureHash[5], generationSignatureHash[4], generationSignatureHash[3], generationSignatureHash[2], generationSignatureHash[1], generationSignatureHash[0]});
// ignore bad block 6134843444354912879
return Generator.verifyHit(hit, BigInteger.valueOf(effectiveBalance), previousBlock, timestamp) || (this.getId()==6134843444354912879L);
} catch (RuntimeException e) {
Logger.logMessage("Error verifying block generation signature", e);
return false;
}
}