org.bitcoinj.core.Utils.bigIntegerToBytes()方法的使用及代码示例

x33g5p2x  于2022-02-01 转载在 其他  
字(9.8k)|赞(0)|评价(0)|浏览(85)

本文整理了Java中org.bitcoinj.core.Utils.bigIntegerToBytes()方法的一些代码示例,展示了Utils.bigIntegerToBytes()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Utils.bigIntegerToBytes()方法的具体详情如下:
包路径:org.bitcoinj.core.Utils
类名称:Utils
方法名:bigIntegerToBytes

Utils.bigIntegerToBytes介绍

[英]The regular java.math.BigInteger#toByteArray() method isn't quite what we often need: it appends a leading zero to indicate that the number is positive and may need padding.
[中]普通的java。数学BigInteger#toByteArray()方法并不是我们经常需要的方法:它会在前导的零后面加上一个数字,表示该数字为正,可能需要填充。

代码示例

代码示例来源:origin: cash.bitcoinj/bitcoinj-core

/**
 * Returns a 32 byte array containing the private key.
 * @throws org.bitcoinj.core.ECKey.MissingPrivateKeyException if the private key bytes are missing/encrypted.
 */
public byte[] getPrivKeyBytes() {
  return Utils.bigIntegerToBytes(getPrivKey(), 32);
}

代码示例来源:origin: greenaddress/GreenBits

/**
 * Returns a 32 byte array containing the private key.
 * @throws org.bitcoinj.core.ECKey.MissingPrivateKeyException if the private key bytes are missing/encrypted.
 */
public byte[] getPrivKeyBytes() {
  return Utils.bigIntegerToBytes(getPrivKey(), 32);
}

代码示例来源:origin: fr.acinq/bitcoinj-core

/**
 * Returns a 32 byte array containing the private key.
 * @throws org.bitcoinj.core.ECKey.MissingPrivateKeyException if the private key bytes are missing/encrypted.
 */
public byte[] getPrivKeyBytes() {
  return Utils.bigIntegerToBytes(getPrivKey(), 32);
}

代码示例来源:origin: HashEngineering/dashj

/**
 * Returns a 32 byte array containing the private key.
 * @throws org.bitcoinj.core.ECKey.MissingPrivateKeyException if the private key bytes are missing/encrypted.
 */
public byte[] getPrivKeyBytes() {
  return Utils.bigIntegerToBytes(getPrivKey(), 32);
}

代码示例来源:origin: fr.acinq/bitcoinj-core

/**
 * Signs a text message using the standard Bitcoin messaging signing format and returns the signature as a base64
 * encoded string.
 *
 * @throws IllegalStateException if this ECKey does not have the private part.
 * @throws KeyCrypterException if this ECKey is encrypted and no AESKey is provided or it does not decrypt the ECKey.
 */
public String signMessage(String message, @Nullable KeyParameter aesKey) throws KeyCrypterException {
  byte[] data = Utils.formatMessageForSigning(message);
  Sha256Hash hash = Sha256Hash.twiceOf(data);
  ECDSASignature sig = sign(hash, aesKey);
  // Now we have to work backwards to figure out the recId needed to recover the signature.
  int recId = -1;
  for (int i = 0; i < 4; i++) {
    ECKey k = ECKey.recoverFromSignature(i, sig, hash, isCompressed());
    if (k != null && k.pub.equals(pub)) {
      recId = i;
      break;
    }
  }
  if (recId == -1)
    throw new RuntimeException("Could not construct a recoverable key. This should never happen.");
  int headerByte = recId + 27 + (isCompressed() ? 4 : 0);
  byte[] sigData = new byte[65];  // 1 header + 32 bytes for R + 32 bytes for S
  sigData[0] = (byte)headerByte;
  System.arraycopy(Utils.bigIntegerToBytes(sig.r, 32), 0, sigData, 1, 32);
  System.arraycopy(Utils.bigIntegerToBytes(sig.s, 32), 0, sigData, 33, 32);
  return new String(Base64.encode(sigData), Charset.forName("UTF-8"));
}

代码示例来源:origin: cash.bitcoinj/bitcoinj-core

/**
 * Signs a text message using the standard Bitcoin messaging signing format and returns the signature as a base64
 * encoded string.
 *
 * @throws IllegalStateException if this ECKey does not have the private part.
 * @throws KeyCrypterException if this ECKey is encrypted and no AESKey is provided or it does not decrypt the ECKey.
 */
public String signMessage(String message, @Nullable KeyParameter aesKey) throws KeyCrypterException {
  byte[] data = Utils.formatMessageForSigning(message);
  Sha256Hash hash = Sha256Hash.twiceOf(data);
  ECDSASignature sig = sign(hash, aesKey);
  // Now we have to work backwards to figure out the recId needed to recover the signature.
  int recId = -1;
  for (int i = 0; i < 4; i++) {
    ECKey k = ECKey.recoverFromSignature(i, sig, hash, isCompressed());
    if (k != null && k.pub.equals(pub)) {
      recId = i;
      break;
    }
  }
  if (recId == -1)
    throw new RuntimeException("Could not construct a recoverable key. This should never happen.");
  int headerByte = recId + 27 + (isCompressed() ? 4 : 0);
  byte[] sigData = new byte[65];  // 1 header + 32 bytes for R + 32 bytes for S
  sigData[0] = (byte)headerByte;
  System.arraycopy(Utils.bigIntegerToBytes(sig.r, 32), 0, sigData, 1, 32);
  System.arraycopy(Utils.bigIntegerToBytes(sig.s, 32), 0, sigData, 33, 32);
  return new String(Base64.encode(sigData), Charset.forName("UTF-8"));
}

代码示例来源:origin: greenaddress/GreenBits

/**
 * Signs a text message using the standard Bitcoin messaging signing format and returns the signature as a base64
 * encoded string.
 *
 * @throws IllegalStateException if this ECKey does not have the private part.
 * @throws KeyCrypterException if this ECKey is encrypted and no AESKey is provided or it does not decrypt the ECKey.
 */
public String signMessage(String message, @Nullable KeyParameter aesKey) throws KeyCrypterException {
  byte[] data = Utils.formatMessageForSigning(message);
  Sha256Hash hash = Sha256Hash.twiceOf(data);
  ECDSASignature sig = sign(hash, aesKey);
  // Now we have to work backwards to figure out the recId needed to recover the signature.
  int recId = -1;
  for (int i = 0; i < 4; i++) {
    ECKey k = ECKey.recoverFromSignature(i, sig, hash, isCompressed());
    if (k != null && k.pub.equals(pub)) {
      recId = i;
      break;
    }
  }
  if (recId == -1)
    throw new RuntimeException("Could not construct a recoverable key. This should never happen.");
  int headerByte = recId + 27 + (isCompressed() ? 4 : 0);
  byte[] sigData = new byte[65];  // 1 header + 32 bytes for R + 32 bytes for S
  sigData[0] = (byte)headerByte;
  System.arraycopy(Utils.bigIntegerToBytes(sig.r, 32), 0, sigData, 1, 32);
  System.arraycopy(Utils.bigIntegerToBytes(sig.s, 32), 0, sigData, 33, 32);
  return new String(Base64.encode(sigData), Charset.forName("UTF-8"));
}

代码示例来源:origin: HashEngineering/dashj

/**
 * Signs a text message using the standard Bitcoin messaging signing format and returns the signature as a base64
 * encoded string.
 *
 * @throws IllegalStateException if this ECKey does not have the private part.
 * @throws KeyCrypterException if this ECKey is encrypted and no AESKey is provided or it does not decrypt the ECKey.
 */
public String signMessage(String message, @Nullable KeyParameter aesKey) throws KeyCrypterException {
  byte[] data = Utils.formatMessageForSigning(message);
  Sha256Hash hash = Sha256Hash.twiceOf(data);
  ECDSASignature sig = sign(hash, aesKey);
  // Now we have to work backwards to figure out the recId needed to recover the signature.
  int recId = -1;
  for (int i = 0; i < 4; i++) {
    ECKey k = ECKey.recoverFromSignature(i, sig, hash, isCompressed());
    if (k != null && k.pub.equals(pub)) {
      recId = i;
      break;
    }
  }
  if (recId == -1)
    throw new RuntimeException("Could not construct a recoverable key. This should never happen.");
  int headerByte = recId + 27 + (isCompressed() ? 4 : 0);
  byte[] sigData = new byte[65];  // 1 header + 32 bytes for R + 32 bytes for S
  sigData[0] = (byte)headerByte;
  System.arraycopy(Utils.bigIntegerToBytes(sig.r, 32), 0, sigData, 1, 32);
  System.arraycopy(Utils.bigIntegerToBytes(sig.s, 32), 0, sigData, 33, 32);
  return new String(Base64.encode(sigData), Charset.forName("UTF-8"));
}

代码示例来源:origin: fr.acinq/bitcoinj-core

protected ECDSASignature doSign(Sha256Hash input, BigInteger privateKeyForSigning) {
  if (Secp256k1Context.isEnabled()) {
    try {
      byte[] signature = NativeSecp256k1.sign(
          input.getBytes(),
          Utils.bigIntegerToBytes(privateKeyForSigning, 32)
      );
      return ECDSASignature.decodeFromDER(signature);
    } catch (NativeSecp256k1Util.AssertFailException e) {
      log.error("Caught AssertFailException inside secp256k1", e);
      throw new RuntimeException(e);
    }
  }
  if (FAKE_SIGNATURES)
    return TransactionSignature.dummy();
  checkNotNull(privateKeyForSigning);
  ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest()));
  ECPrivateKeyParameters privKey = new ECPrivateKeyParameters(privateKeyForSigning, CURVE);
  signer.init(true, privKey);
  BigInteger[] components = signer.generateSignature(input.getBytes());
  return new ECDSASignature(components[0], components[1]).toCanonicalised();
}

代码示例来源:origin: cash.bitcoinj/bitcoinj-core

protected ECDSASignature doSign(Sha256Hash input, BigInteger privateKeyForSigning) {
  if (Secp256k1Context.isEnabled()) {
    try {
      byte[] signature = NativeSecp256k1.sign(
          input.getBytes(),
          Utils.bigIntegerToBytes(privateKeyForSigning, 32)
      );
      return ECDSASignature.decodeFromDER(signature);
    } catch (NativeSecp256k1Util.AssertFailException e) {
      log.error("Caught AssertFailException inside secp256k1", e);
      throw new RuntimeException(e);
    }
  }
  if (FAKE_SIGNATURES)
    return TransactionSignature.dummy();
  checkNotNull(privateKeyForSigning);
  ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest()));
  ECPrivateKeyParameters privKey = new ECPrivateKeyParameters(privateKeyForSigning, CURVE);
  signer.init(true, privKey);
  BigInteger[] components = signer.generateSignature(input.getBytes());
  return new ECDSASignature(components[0], components[1]).toCanonicalised();
}

代码示例来源:origin: greenaddress/GreenBits

protected ECDSASignature doSign(Sha256Hash input, BigInteger privateKeyForSigning) {
  if (Secp256k1Context.isEnabled()) {
    try {
      byte[] signature = NativeSecp256k1.sign(
          input.getBytes(),
          Utils.bigIntegerToBytes(privateKeyForSigning, 32)
      );
      return ECDSASignature.decodeFromDER(signature);
    } catch (NativeSecp256k1Util.AssertFailException e) {
      log.error("Caught AssertFailException inside secp256k1", e);
      throw new RuntimeException(e);
    }
  }
  if (FAKE_SIGNATURES)
    return TransactionSignature.dummy();
  checkNotNull(privateKeyForSigning);
  ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest()));
  ECPrivateKeyParameters privKey = new ECPrivateKeyParameters(privateKeyForSigning, CURVE);
  signer.init(true, privKey);
  BigInteger[] components = signer.generateSignature(input.getBytes());
  return new ECDSASignature(components[0], components[1]).toCanonicalised();
}

代码示例来源:origin: HashEngineering/dashj

protected ECDSASignature doSign(Sha256Hash input, BigInteger privateKeyForSigning) {
  if (Secp256k1Context.isEnabled()) {
    try {
      byte[] signature = NativeSecp256k1.sign(
          input.getBytes(),
          Utils.bigIntegerToBytes(privateKeyForSigning, 32)
      );
      return ECDSASignature.decodeFromDER(signature);
    } catch (NativeSecp256k1Util.AssertFailException e) {
      log.error("Caught AssertFailException inside secp256k1", e);
      throw new RuntimeException(e);
    }
  }
  if (FAKE_SIGNATURES)
    return TransactionSignature.dummy();
  checkNotNull(privateKeyForSigning);
  ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest()));
  ECPrivateKeyParameters privKey = new ECPrivateKeyParameters(privateKeyForSigning, CURVE);
  signer.init(true, privKey);
  BigInteger[] components = signer.generateSignature(input.getBytes());
  return new ECDSASignature(components[0], components[1]).toCanonicalised();
}

相关文章