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

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

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

Utils.formatMessageForSigning介绍

[英]Given a textual message, returns a byte buffer formatted as follows:

[24] "Bitcoin Signed Message:\n" [message.length as a varint] message
[中]给定文本消息,返回字节缓冲区,格式如下:
[24]“比特币签名信息:\n”[Message.length as a varint]信息

代码示例

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

public static boolean verifyMessage(byte [] pubkeyId, MasternodeSignature vchSig, String message,
                    StringBuilder errorMessage) {
    byte [] dataToHash = Utils.formatMessageForSigning(message);
    return HashSigner.verifyHash(Sha256Hash.twiceOf(dataToHash), pubkeyId, vchSig, errorMessage);
  }
}

代码示例来源:origin: blockchain/unused-My-Wallet-V3-jar

private static byte[] magicHash(byte[] message) {
  byte[] messageBytes = Utils.formatMessageForSigning(Base64Util.encodeBase64String(message));
  return Sha256Hash.hashTwice(messageBytes);
}

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

public static ECKey signedMessageToKey(byte [] message, byte [] signatureEncoded) throws SignatureException {
  // Parse the signature bytes into r/s and the selector value.
  if (signatureEncoded.length < 65)
    throw new SignatureException("Signature truncated, expected 65 bytes and got " + signatureEncoded.length);
  int header = signatureEncoded[0] & 0xFF;
  // The header byte: 0x1B = first key with even y, 0x1C = first key with odd y,
  //                  0x1D = second key with even y, 0x1E = second key with odd y
  if (header < 27 || header > 34)
    throw new SignatureException("Header byte out of range: " + header);
  BigInteger r = new BigInteger(1, Arrays.copyOfRange(signatureEncoded, 1, 33));
  BigInteger s = new BigInteger(1, Arrays.copyOfRange(signatureEncoded, 33, 65));
  ECDSASignature sig = new ECDSASignature(r, s);
  byte[] messageBytes = Utils.formatMessageForSigning(message);
  // Note that the C++ code doesn't actually seem to specify any character encoding. Presumably it's whatever
  // JSON-SPIRIT hands back. Assume UTF-8 for now.
  Sha256Hash messageHash = Sha256Hash.twiceOf(messageBytes);
  boolean compressed = false;
  if (header >= 31) {
    compressed = true;
    header -= 4;
  }
  int recId = header - 27;
  ECKey key = ECKey.recoverFromSignature(recId, sig, messageHash, compressed);
  if (key == null)
    throw new SignatureException("Could not recover public key from signature");
  return key;
}

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

public static boolean verifyMessage1(PublicKey pubkey, MasternodeSignature vchSig, byte[] message, StringBuilder errorMessage)
{
  //int length = Utils.BITCOIN_SIGNED_MESSAGE_HEADER.length()+strMessage.length();
  byte dataToHash []; // = (Utils.BITCOIN_SIGNED_MESSAGE_HEADER_BYTES+strMessage).getBytes();
  //ByteOutputStream bos = new ByteOutputStream(message.length + Utils.BITCOIN_SIGNED_MESSAGE_HEADER_BYTES.length);
  //bos.write(Utils.BITCOIN_SIGNED_MESSAGE_HEADER_BYTES);
  //bos.write(message);
  dataToHash = Utils.formatMessageForSigning(message);//bos.getBytes();
  //PublicKey pubkey2;
  ECKey pubkey2 = null;
  try {
    // pubkey2 = PublicKey.recoverCompact(Sha256Hash.twiceOf(dataToHash), vchSig);
    //ECKey.verify()
    //if(DarkCoinSystem.fDebug && !pubkey.getId().equals(pubkey2.getId()));
    //    log.info("DarkSendSigner.verifyMessage -- keys don't match: " + pubkey2.getId().toString()+ " " + pubkey.getId().toString());
    //return pubkey.getId().equals(pubkey2.getId());
    //return true;
    pubkey2 = ECKey.fromPublicOnly(pubkey.getBytes());
    pubkey2.verifyMessage(message, vchSig.getBytes());
    return true;
  }
  catch(SignatureException x)
  {
    errorMessage.append("keys don't match - input: "+Utils.HEX.encode(pubkey.getId()));
    errorMessage.append(", recovered: " + (pubkey2 != null ? Utils.HEX.encode(pubkey2.getPubKeyHash()) : "null"));
    errorMessage.append(", message: "+ Utils.sanitizeString(new String(message)));
    errorMessage.append(", sig:  not impl!\n" + x.getMessage());
    return false;
  }
}

代码示例来源: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: 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: 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

BigInteger s = new BigInteger(1, Arrays.copyOfRange(signatureEncoded, 33, 65));
ECDSASignature sig = new ECDSASignature(r, s);
byte[] messageBytes = Utils.formatMessageForSigning(message);

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

BigInteger s = new BigInteger(1, Arrays.copyOfRange(signatureEncoded, 33, 65));
ECDSASignature sig = new ECDSASignature(r, s);
byte[] messageBytes = Utils.formatMessageForSigning(message);

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

BigInteger s = new BigInteger(1, Arrays.copyOfRange(signatureEncoded, 33, 65));
ECDSASignature sig = new ECDSASignature(r, s);
byte[] messageBytes = Utils.formatMessageForSigning(message);

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

BigInteger s = new BigInteger(1, Arrays.copyOfRange(signatureEncoded, 33, 65));
ECDSASignature sig = new ECDSASignature(r, s);
byte[] messageBytes = Utils.formatMessageForSigning(message);

相关文章