• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Java Keccak类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Java中org.bouncycastle.jcajce.provider.digest.Keccak的典型用法代码示例。如果您正苦于以下问题:Java Keccak类的具体用法?Java Keccak怎么用?Java Keccak使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



Keccak类属于org.bouncycastle.jcajce.provider.digest包,在下文中一共展示了Keccak类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。

示例1: getTransactionHash

import org.bouncycastle.jcajce.provider.digest.Keccak; //导入依赖的package包/类
/***
 * Calculates the hash of a transaction. Note this requires that you have Bouncy castle as a dependency in your project
 * 
 * @param eTrans transaction
 * @return transaction hash as KECCAK-256
 */
public static byte[] getTransactionHash(EthereumTransaction eTrans) {
	ArrayList<byte[]> rlpTransaction = new ArrayList<>();
	rlpTransaction.add(EthereumUtil.encodeRLPElement(eTrans.getNonce()));
	rlpTransaction.add(EthereumUtil.encodeRLPElement(EthereumUtil.convertLongToVarInt(eTrans.getGasPrice())));
	rlpTransaction.add(EthereumUtil.encodeRLPElement(EthereumUtil.convertLongToVarInt(eTrans.getGasLimit())));
	rlpTransaction.add(EthereumUtil.encodeRLPElement(eTrans.getReceiveAddress()));
	rlpTransaction.add(EthereumUtil.encodeRLPElement(EthereumUtil.convertLongToVarInt(eTrans.getValue())));
	rlpTransaction.add(EthereumUtil.encodeRLPElement(eTrans.getData()));
	rlpTransaction.add(EthereumUtil.encodeRLPElement(eTrans.getSig_v()));
	rlpTransaction.add(EthereumUtil.encodeRLPElement(eTrans.getSig_r()));
	rlpTransaction.add(EthereumUtil.encodeRLPElement(eTrans.getSig_s()));
	byte[] transEnc = EthereumUtil.encodeRLPList(rlpTransaction);
	Keccak.Digest256 digest = new Keccak.Digest256();
	digest.update(transEnc,0,transEnc.length);
	return digest.digest();
}
 
开发者ID:ZuInnoTe,项目名称:hadoopcryptoledger,代码行数:23,代码来源:EthereumUtil.java


示例2: isValidChecksum

import org.bouncycastle.jcajce.provider.digest.Keccak; //导入依赖的package包/类
/**
 * Checks if the given string is an address with checksum (Keccak256).
 *
 * @param address a wallet address ('0x...')
 * @return {@code true} if address with checksum
 */
static boolean isValidChecksum(String address) {
    Keccak.Digest256 digest256 = new Keccak.Digest256();

    digest256.update(
            address.substring(2).toLowerCase().getBytes());

    String hash = Hex.toHexString(digest256.digest());

    for (int i = 0; i < 40; i++) {
        char ch = address.charAt(i + 2);
        int dg = Character.digit(hash.charAt(i), 16);

        if ((dg > 7 && Character.toUpperCase(ch) != ch)
                || (dg <= 7 && Character.toLowerCase(ch) != ch))
            return false;
    }

    return true;
}
 
开发者ID:infinitape,项目名称:etherjar,代码行数:26,代码来源:Address.java


示例3: sha3

import org.bouncycastle.jcajce.provider.digest.Keccak; //导入依赖的package包/类
public static byte[] sha3(byte[]... input) {
    Keccak.DigestKeccak kecc = new Keccak.Digest256();
    for (byte[] bytes : input) {
        kecc.update(bytes, 0, bytes.length);
    }
    return kecc.digest();
}
 
开发者ID:papyrusglobal,项目名称:state-channels,代码行数:8,代码来源:CryptoUtil.java


示例4: getTransactionHashWithoutSignature

import org.bouncycastle.jcajce.provider.digest.Keccak; //导入依赖的package包/类
/***
 * Calculates the hash of a transaction without signature. Note this requires that you have Bouncy castle as a dependency in your project
 * 
 * @param eTrans transaction
 * @return transaction hash as KECCAK-256
 */
public static byte[] getTransactionHashWithoutSignature(EthereumTransaction eTrans) {
	ArrayList<byte[]> rlpTransaction = new ArrayList<>();
	rlpTransaction.add(EthereumUtil.encodeRLPElement(eTrans.getNonce()));
	rlpTransaction.add(EthereumUtil.encodeRLPElement(EthereumUtil.convertLongToVarInt(eTrans.getGasPrice())));
	rlpTransaction.add(EthereumUtil.encodeRLPElement(EthereumUtil.convertLongToVarInt(eTrans.getGasLimit())));
	rlpTransaction.add(EthereumUtil.encodeRLPElement(eTrans.getReceiveAddress()));
	rlpTransaction.add(EthereumUtil.encodeRLPElement(EthereumUtil.convertLongToVarInt(eTrans.getValue())));
	rlpTransaction.add(EthereumUtil.encodeRLPElement(eTrans.getData()));
	byte[] transEnc = EthereumUtil.encodeRLPList(rlpTransaction);
	Keccak.Digest256 digest = new Keccak.Digest256();
	digest.update(transEnc,0,transEnc.length);
	return digest.digest();
}
 
开发者ID:ZuInnoTe,项目名称:hadoopcryptoledger,代码行数:20,代码来源:EthereumUtil.java


示例5: fromSignature

import org.bouncycastle.jcajce.provider.digest.Keccak; //导入依赖的package包/类
public static MethodId fromSignature(String name, Collection<String> types) {
    String sign = Objects.requireNonNull(name) +
            '(' + String.join(",", Objects.requireNonNull(types)) + ')';

    byte[] head = new byte[SIZE_BYTES];
    Keccak.Digest256 digest256 = new Keccak.Digest256();

    digest256.update(sign.getBytes());
    System.arraycopy(digest256.digest(), 0, head, 0, SIZE_BYTES);

    return from(head);
}
 
开发者ID:infinitape,项目名称:etherjar,代码行数:13,代码来源:MethodId.java


示例6: toChecksumString

import org.bouncycastle.jcajce.provider.digest.Keccak; //导入依赖的package包/类
public String toChecksumString() {
    Keccak.Digest256 digest256 = new Keccak.Digest256();

    byte[] hex = new byte[value.length * 2];
    for(int i = 0, j = 0; i < value.length; i++){
        hex[j++] = HEX_BYTES[(0xF0 & value[i]) >>> 4];
        hex[j++] = HEX_BYTES[0x0F & value[i]];
    }
    digest256.update(hex);
    String hash = Hex.toHexString(digest256.digest());

    char[] plain = toHex().toCharArray();
    char[] str = new char[hex.length + 2];
    if (plain.length != str.length) {
        throw new IllegalStateException("Hex representation has invalid length: " + plain.length + " != " + str.length);
    }
    str[0] = '0';
    str[1] = 'x';
    for (int i = 0; i < 40; i++) {
        int dg = Character.digit(hash.charAt(i), 16);
        if (dg > 7) {
            str[i + 2] = Character.toUpperCase(plain[i + 2]);
        } else {
            str[i + 2] = plain[i + 2];
        }
    }
    return new String(str);
}
 
开发者ID:infinitape,项目名称:etherjar,代码行数:29,代码来源:Address.java


示例7: hashKeccak

import org.bouncycastle.jcajce.provider.digest.Keccak; //导入依赖的package包/类
/**
 * Generates a SHA-3 hash on the provided data.
 *
 * @param data Hex data encoded in a string.
 * @return Hash encoded in a hex string.
 */
public static String hashKeccak(String data) {
  byte[] dataBytes = ByteUtilities.toByteArray(data);
  Keccak.DigestKeccak md = new Keccak.DigestKeccak(256);
  md.reset();
  md.update(dataBytes, 0, dataBytes.length);
  byte[] hashedBytes = md.digest();
  return ByteUtilities.toHexString(hashedBytes);
}
 
开发者ID:Braveno,项目名称:cosigner,代码行数:15,代码来源:EthereumTools.java


示例8: getSendAddress

import org.bouncycastle.jcajce.provider.digest.Keccak; //导入依赖的package包/类
/**
 * Calculates the sent address of an EthereumTransaction. Note this can be a costly operation to calculate. . This requires that you have Bouncy castle as a dependency in your project
 *
 *
 * @param eTrans transaction
 * @return sent address as byte array
 */
public static byte[] getSendAddress(EthereumTransaction eTrans) {
	// init, maybe we move this out to save time
	X9ECParameters params = SECNamedCurves.getByName("secp256k1");
	ECDomainParameters CURVE=new ECDomainParameters(params.getCurve(), params.getG(), params.getN(), params.getH());	 // needed for getSentAddress

  // transaction hash without signature data
	byte[] transactionHash = EthereumUtil.getTransactionHashWithoutSignature(eTrans);
  // signature to address
	BigInteger bR = new BigInteger(1,eTrans.getSig_r());
	BigInteger bS = new BigInteger(1,eTrans.getSig_s());
  // calculate v for signature
	byte v =(byte) (eTrans.getSig_v()[0]);
	if (!((v == EthereumUtil.LOWER_REAL_V) || (v== (LOWER_REAL_V+1)))) {
		v = EthereumUtil.LOWER_REAL_V;
		if (((int)v%2 == 0)) {
			v = (byte) (v+0x01);
		}
	}
	
	boolean compressedKey= false;
	// the following lines are inspired from ECKey.java of EthereumJ, but adapted to the hadoopcryptoledger context
	if (v < 27 || v > 34) {
		throw new RuntimeException("Header out of range");
	}
	if (v>=31) {
		compressedKey = true;
		v -=4;
	}
	int receiverId = v - 27;
	BigInteger n = CURVE.getN();
    BigInteger i = BigInteger.valueOf((long) receiverId / 2);
    BigInteger x = bR.add(i.multiply(n));
    ECCurve.Fp curve = (ECCurve.Fp) CURVE.getCurve();
    BigInteger prime = curve.getQ();
    if (x.compareTo(prime) >= 0) {
        return null;
     }
    // decompress Key
    X9IntegerConverter x9 = new X9IntegerConverter();
    byte[] compEnc = x9.integerToBytes(x, 1 + x9.getByteLength(CURVE.getCurve()));
    boolean yBit=(receiverId & 1) == 1;
    compEnc[0] = (byte)(yBit ? 0x03 : 0x02);
    ECPoint R =  CURVE.getCurve().decodePoint(compEnc);
    if (!R.multiply(n).isInfinity()) {
    		return null;
    }
    BigInteger e = new BigInteger(1,transactionHash);
    BigInteger eInv = BigInteger.ZERO.subtract(e).mod(n);
    BigInteger rInv = bR.modInverse(n);
    BigInteger srInv = rInv.multiply(bS).mod(n);
    BigInteger eInvrInv = rInv.multiply(eInv).mod(n);
    ECPoint.Fp q = (ECPoint.Fp) ECAlgorithms.sumOfTwoMultiplies(CURVE.getG(), eInvrInv, R, srInv);
    byte[] pubKey=q.getEncoded(false);
    // now we need to convert the public key into an ethereum sent address which is the last 20 bytes of 32 byte KECCAK-256 Hash of the key.
	Keccak.Digest256 digest256 = new Keccak.Digest256();
	digest256.update(pubKey,1,pubKey.length-1);
	byte[] kcck = digest256.digest();
    return Arrays.copyOfRange(kcck,12,kcck.length);
}
 
开发者ID:ZuInnoTe,项目名称:hadoopcryptoledger,代码行数:67,代码来源:EthereumUtil.java


示例9: sha3

import org.bouncycastle.jcajce.provider.digest.Keccak; //导入依赖的package包/类
/**
 * Keccak-256 hash function.
 *
 * @param input binary encoded input data
 * @param offset of start of data
 * @param length of data
 * @return hash value
 */
public static byte[] sha3(byte[] input, int offset, int length) {
    Keccak.DigestKeccak kecc = new Keccak.Digest256();
    kecc.update(input, offset, length);
    return kecc.digest();
}
 
开发者ID:web3j,项目名称:web3j,代码行数:14,代码来源:Hash.java



注:本文中的org.bouncycastle.jcajce.provider.digest.Keccak类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Java RestMethod类代码示例发布时间:2022-05-23
下一篇:
Java AnimationAdapter类代码示例发布时间:2022-05-23
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap