org.spongycastle.crypto.macs.HMac类的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(6.6k)|赞(0)|评价(0)|浏览(149)

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

HMac介绍

[英]HMAC implementation based on RFC2104 H(K XOR opad, H(K XOR ipad, text))
[中]基于rfc2104h(K-XOR-opad,H(K-XOR-ipad,text))的HMAC实现

代码示例

代码示例来源:origin: ethereum/ethereumj

private static EthereumIESEngine makeIESEngine(boolean isEncrypt, ECPoint pub, BigInteger prv, byte[] IV) {
  AESEngine aesFastEngine = new AESEngine();
  EthereumIESEngine iesEngine = new EthereumIESEngine(
      new ECDHBasicAgreement(),
      new ConcatKDFBytesGenerator(new SHA256Digest()),
      new HMac(new SHA256Digest()),
      new SHA256Digest(),
      new BufferedBlockCipher(new SICBlockCipher(aesFastEngine)));
  byte[]         d = new byte[] {};
  byte[]         e = new byte[] {};
  IESParameters p = new IESWithCipherParameters(d, e, KEY_SIZE, KEY_SIZE);
  ParametersWithIV parametersWithIV = new ParametersWithIV(p, IV);
  iesEngine.init(isEncrypt, new ECPrivateKeyParameters(prv, CURVE), new ECPublicKeyParameters(pub, CURVE), parametersWithIV);
  return iesEngine;
}

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

static byte[] hmacSha512(HMac hmacSha512, byte[] input) {
  hmacSha512.reset();
  hmacSha512.update(input, 0, input.length);
  byte[] out = new byte[64];
  hmacSha512.doFinal(out, 0);
  return out;
}

代码示例来源:origin: com.madgag.spongycastle/core

/**
 * Base constructor.
 *
 * @param digest digest to build the HMAC on.
 */
public HMacDSAKCalculator(Digest digest)
{
  this.hMac = new HMac(digest);
  this.V = new byte[hMac.getMacSize()];
  this.K = new byte[hMac.getMacSize()];
}

代码示例来源:origin: com.madgag.spongycastle/pkix

public byte[] getMac()
{
  byte[] res = new byte[hMac.getMacSize()];
  hMac.doFinal(res, 0);
  return res;
}

代码示例来源:origin: com.madgag.spongycastle/bctls-jdk15on

protected void hmacHash(Digest digest, byte[] secret, int secretOff, int secretLen, byte[] seed, byte[] output)
{
  HMac mac = new HMac(digest);
  mac.init(new KeyParameter(secret, secretOff, secretLen));
  byte[] a = seed;
  int macSize = mac.getMacSize();
  byte[] b1 = new byte[macSize];
  byte[] b2 = new byte[macSize];
  int pos = 0;
  while (pos < output.length)
  {
    mac.update(a, 0, a.length);
    mac.doFinal(b1, 0);
    a = b1;
    mac.update(a, 0, a.length);
    mac.update(seed, 0, seed.length);
    mac.doFinal(b2, 0);
    System.arraycopy(b2, 0, output, pos, Math.min(macSize, output.length - pos));
    pos += macSize;
  }
}

代码示例来源:origin: com.google/bitcoinj

static HMac createHmacSha512Digest(byte[] key) {
  SHA512Digest digest = new SHA512Digest();
  HMac hMac = new HMac(digest);
  hMac.init(new KeyParameter(key));
  return hMac;
}

代码示例来源:origin: com.madgag.spongycastle/core

public BigInteger nextK()
{
  byte[] t = new byte[((n.bitLength() + 7) / 8)];
  for (;;)
  {
    int tOff = 0;
    while (tOff < t.length)
    {
      hMac.update(V, 0, V.length);
      hMac.doFinal(V, 0);
      int len = Math.min(t.length - tOff, V.length);
      System.arraycopy(V, 0, t, tOff, len);
      tOff += len;
    }
    BigInteger k = bitsToInt(t);
    if (k.compareTo(ZERO) > 0 && k.compareTo(n) < 0)
    {
      return k;
    }
    hMac.update(V, 0, V.length);
    hMac.update((byte)0x00);
    hMac.doFinal(K, 0);
    hMac.init(new KeyParameter(K));
    hMac.update(V, 0, V.length);
    hMac.doFinal(V, 0);
  }
}

代码示例来源:origin: OPCFoundation/UA-Java-Legacy

byte[] computedSignature = new byte[hmac.getMacSize()];
hmac.update(dataToVerify, inputOffset, verifyLen);
hmac.doFinal(computedSignature, 0);

代码示例来源:origin: com.madgag.spongycastle/core

/**
 * Performs the expand part of the key derivation function, using currentT
 * as input and output buffer.
 *
 * @throws DataLengthException if the total number of bytes generated is larger than the one
 * specified by RFC 5869 (255 * HashLen)
 */
private void expandNext()
  throws DataLengthException
{
  int n = generatedBytes / hashLen + 1;
  if (n >= 256)
  {
    throw new DataLengthException(
      "HKDF cannot generate more than 255 blocks of HashLen size");
  }
  // special case for T(0): T(0) is empty, so no update
  if (generatedBytes != 0)
  {
    hMacHash.update(currentT, 0, hashLen);
  }
  hMacHash.update(info, 0, info.length);
  hMacHash.update((byte)n);
  hMacHash.doFinal(currentT, 0);
}

代码示例来源:origin: com.madgag.spongycastle/bctls-jdk15on

public void setKey(byte[] key, int keyOff, int keyLen)
{
  hmac.init(new KeyParameter(key, keyOff, keyLen));
}

代码示例来源:origin: com.madgag.spongycastle/bctls-jdk15on

public void update(byte[] input, int inOff, int length)
{
  hmac.update(input, inOff, length);
}

代码示例来源:origin: com.madgag.spongycastle/bctls-jdk15on

public int getMacLength()
{
  return hmac.getMacSize();
}

代码示例来源:origin: com.madgag.spongycastle/bctls-jdk15on

public void reset()
  {
    hmac.reset();
  }
}

代码示例来源:origin: com.madgag.spongycastle/core

static void hmac_hash(Digest digest, byte[] secret, byte[] seed, byte[] out)
{
  HMac mac = new HMac(digest);
  mac.init(new KeyParameter(secret));
  byte[] a = seed;
  int size = digest.getDigestSize();
  int iterations = (out.length + size - 1) / size;
  byte[] buf = new byte[mac.getMacSize()];
  byte[] buf2 = new byte[mac.getMacSize()];
  for (int i = 0; i < iterations; i++)
  {
    mac.update(a, 0, a.length);
    mac.doFinal(buf, 0);
    a = buf;
    mac.update(a, 0, a.length);
    mac.update(seed, 0, seed.length);
    mac.doFinal(buf2, 0);
    System.arraycopy(buf2, 0, out, (size * i), Math.min(size, out.length - (size * i)));
  }
}

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

static HMac createHmacSha512Digest(byte[] key) {
  SHA512Digest digest = new SHA512Digest();
  HMac hMac = new HMac(digest);
  hMac.init(new KeyParameter(key));
  return hMac;
}

代码示例来源:origin: com.madgag.spongycastle/core

/**
 * Performs the extract part of the key derivation function.
 *
 * @param salt the salt to use
 * @param ikm  the input keying material
 * @return the PRK as KeyParameter
 */
private KeyParameter extract(byte[] salt, byte[] ikm)
{
  if (salt == null)
  {
    // TODO check if hashLen is indeed same as HMAC size
    hMacHash.init(new KeyParameter(new byte[hashLen]));
  }
  else
  {
    hMacHash.init(new KeyParameter(salt));
  }
  hMacHash.update(ikm, 0, ikm.length);
  byte[] prk = new byte[hashLen];
  hMacHash.doFinal(prk, 0);
  return new KeyParameter(prk);
}

代码示例来源:origin: com.madgag.spongycastle/bctls-jdk15on

public byte[] calculateMAC()
{
  byte[] rv = new byte[hmac.getMacSize()];
  hmac.doFinal(rv, 0);
  return rv;
}

代码示例来源:origin: OPCFoundation/UA-Java-Legacy

/** {@inheritDoc} */
@Override
public void signSymm(SecurityPolicy policy, byte[] key, byte[] input, int inputOffset, int verifyLen,
    byte[] output, int outputOffset) throws ServiceResultException {
  HMac hmac = createMac(policy.getSymmetricSignatureAlgorithm(), new KeyParameter(key));
  hmac.update(input, inputOffset, verifyLen);
  hmac.doFinal(output, outputOffset);
}

代码示例来源:origin: com.madgag.spongycastle/core

public void init(DerivationParameters param)
{
  if (!(param instanceof HKDFParameters))
  {
    throw new IllegalArgumentException(
      "HKDF parameters required for HKDFBytesGenerator");
  }
  HKDFParameters params = (HKDFParameters)param;
  if (params.skipExtract())
  {
    // use IKM directly as PRK
    hMacHash.init(new KeyParameter(params.getIKM()));
  }
  else
  {
    hMacHash.init(extract(params.getSalt(), params.getIKM()));
  }
  info = params.getInfo();
  generatedBytes = 0;
  currentT = new byte[hashLen];
}

代码示例来源:origin: com.madgag/sc-light-jdk15on

public int doFinal(
  byte[] out,
  int outOff)
{
  byte[] tmp = new byte[digestSize];
  digest.doFinal(tmp, 0);
  digest.update(outputPad, 0, outputPad.length);
  digest.update(tmp, 0, tmp.length);
  int     len = digest.doFinal(out, outOff);
  reset();
  return len;
}

相关文章