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

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

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

public static byte[] hmacSha512(byte[] key, byte[] input) {
  HMac hMac = new HMac(new SHA512Digest());
  hMac.init(new KeyParameter(key));
  hMac.update(input, 0, input.length);
  byte[] out = new byte[64];
  hMac.doFinal(out, 0);
  return out;
}

代码示例来源:origin: net.jradius/jradius-extended

/**
 * Generate a new instance of an TlsMac.
 * 
 * @param digest The digest to use.
 * @param key_block A byte-array where the key for this mac is located.
 * @param offset The number of bytes to skip, before the key starts in the buffer.
 * @param len The length of the key.
 */
protected TlsMac(Digest digest, byte[] key_block, int offset, int len)
{
  this.mac = new HMac(digest);
  KeyParameter param = new KeyParameter(key_block, offset, len);
  this.mac.init(param);
  this.seqNo = 0;
}

代码示例来源:origin: de.idyl/winzipaes

public void init( String pwStr, int keySize, byte[] salt, byte[] pwVerification ) throws ZipException {
  byte[] pwBytes = pwStr.getBytes();
  
  super.saltBytes = salt;
  PBEParametersGenerator generator = new PKCS5S2ParametersGenerator();
  generator.init( pwBytes, salt, ITERATION_COUNT );
  cipherParameters = generator.generateDerivedParameters(KEY_SIZE_BIT*2 + 16);
  byte[] keyBytes = ((KeyParameter)cipherParameters).getKey();
  this.cryptoKeyBytes = new byte[ KEY_SIZE_BYTE ];
  System.arraycopy( keyBytes, 0, cryptoKeyBytes, 0, KEY_SIZE_BYTE );
  this.authenticationCodeBytes = new byte[ KEY_SIZE_BYTE ];
  System.arraycopy( keyBytes, KEY_SIZE_BYTE, authenticationCodeBytes, 0, KEY_SIZE_BYTE );
  // based on SALT + PASSWORD (password is probably correct)
  this.pwVerificationBytes = new byte[ 2 ];
  System.arraycopy( keyBytes, KEY_SIZE_BYTE*2, this.pwVerificationBytes, 0, 2 );
  if( !ByteArrayHelper.isEqual( this.pwVerificationBytes, pwVerification ) ) {
    throw new ZipException("wrong password - " + ByteArrayHelper.toString(this.pwVerificationBytes) + "/ " + ByteArrayHelper.toString(pwVerification));
  }
  // create the first 16 bytes of the key sequence again (using pw+salt)
  generator.init( pwBytes, salt, ITERATION_COUNT );
  cipherParameters = generator.generateDerivedParameters(KEY_SIZE_BIT);
  // checksum added to the end of the encrypted data, update on each encryption call
  this.mac = new HMac( new SHA1Digest() );
  mac.init( new KeyParameter(authenticationCodeBytes) );
  this.aesCipher = new SICBlockCipher(new AESEngine());
  this.blockSize = aesCipher.getBlockSize();
  // incremented on each 16 byte block and used as encryption NONCE (ivBytes)
  nonce = 1;
}

代码示例来源:origin: org.bouncycastle/bcprov-debug-jdk15on

/**
 * Creates a HKDFBytesGenerator based on the given hash function.
 *
 * @param hash the digest to be used as the source of generatedBytes bytes
 */
public HKDFBytesGenerator(Digest hash)
{
  this.hMacHash = new HMac(hash);
  this.hashLen = hash.getDigestSize();
}

代码示例来源: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: org.xipki.tk/security

private byte[] hmac(final byte[] contentToSign,
    final HashAlgoType hashAlgo) {
  HMac hmac = new HMac(hashAlgo.createDigest());
  hmac.update(contentToSign, 0, contentToSign.length);
  byte[] signature = new byte[hmac.getMacSize()];
  hmac.doFinal(signature, 0);
  return signature;
}

代码示例来源:origin: redfish64/TinyTravelTracker

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: net.jradius/jradius-extended

mac.update(macHeader, 0, macHeader.length);
mac.update(message, offset, len);
byte[] result = new byte[mac.getMacSize()];
mac.doFinal(result, 0);
return result;

代码示例来源:origin: RUB-NDS/TLS-Attacker

public static WrappedMac getMac(ProtocolVersion version, CipherSuite cipherSuite, byte[] key)
    throws NoSuchAlgorithmException {
  MacAlgorithm macAlg = AlgorithmResolver.getMacAlgorithm(version, cipherSuite);
  if (macAlg == MacAlgorithm.HMAC_GOSTR3411) {
    GOST3411Digest digest = new GOST3411Digest();
    return new ContinuousMac(new HMac(digest), digest, new KeyParameter(key));
  } else if (macAlg == MacAlgorithm.HMAC_GOSTR3411_2012_256) {
    GOST3411_2012_256Digest digest = new GOST3411_2012_256Digest();
    return new ContinuousMac(new HMac(digest), digest, new KeyParameter(key));
  } else if (macAlg == MacAlgorithm.IMIT_GOST28147) {
    ParametersWithSBox parameters = new ParametersWithSBox(new KeyParameter(key),
        GOSTUtils.getGostSBox(cipherSuite));
    return new ContinuousMac(new GOST28147Mac(), parameters);
  } else if (macAlg.getJavaName() != null) {
    return new JavaMac(macAlg.getJavaName(), key);
  } else {
    throw new NoSuchAlgorithmException("Mac: " + macAlg + " is not supported!");
  }
}

代码示例来源:origin: stackoverflow.com

Digest  digest = new SHA256Digest();
HMac hmac = new HMac(digest);
hmac.init(new KeyParameter(appKeyHere));
hmac.update(requestURI, 0, lenOfReqURI);
byte[]  resBuf = new byte[digest.getDigestSize()];
hmac.doFinal(resBuf, 0);
String  resStr = new String(Hex.encode(resBuf)); // Contains final usable value

代码示例来源:origin: de.idyl/winzipaes

/** 10 bytes */
public byte[] getFinalAuthentication() {
  // MAC / based on encIn + PASSWORD + SALT (encryption was successful)
  byte[] macBytes = new byte[ mac.getMacSize() ];
  mac.doFinal( macBytes, 0 );
  byte[] macBytes10 = new byte[10];
  System.arraycopy( macBytes, 0, macBytes10, 0, 10 );
  return macBytes10;
}

代码示例来源:origin: org.bouncycastle/bcprov-debug-jdk15on

/**
 * 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: org.xipki/security

@Override
public void write(byte[] bytes, int off, int len) throws IOException {
 hmac.update(bytes, off, len);
}

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

private HMac createMac(SecurityAlgorithm algorithm, KeyParameter param)
    throws ServiceResultException {
  HMac hmac = null;
  if (algorithm.equals(SecurityAlgorithm.HmacSha1)) {
    hmac = new HMac(new SHA1Digest());
  } else if (algorithm.equals(SecurityAlgorithm.HmacSha256)) {
    hmac = new HMac(new SHA256Digest());
  } else {
    throw new ServiceResultException(
        StatusCodes.Bad_SecurityPolicyRejected,
        "Unsupported symmetric signature algorithm: " + algorithm);
  }
  hmac.init(param);
  return hmac;
}

代码示例来源:origin: redfish64/TinyTravelTracker

/**
 * 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.hynnet/jradius-extended

/**
 * @return The Keysize of the mac.
 */
protected int getSize()
{
  return mac.getMacSize();
}

代码示例来源:origin: org.xipki.tk/security

@Override
public byte[] getSignature() {
  byte[] signature = new byte[outLen];
  hmac.doFinal(signature, 0);
  return signature;
}

代码示例来源:origin: com.hynnet/jradius-extended

/**
 * Generate a new instance of an TlsMac.
 * 
 * @param digest The digest to use.
 * @param key_block A byte-array where the key for this mac is located.
 * @param offset The number of bytes to skip, before the key starts in the buffer.
 * @param len The length of the key.
 */
protected TlsMac(Digest digest, byte[] key_block, int offset, int len)
{
  this.mac = new HMac(digest);
  KeyParameter param = new KeyParameter(key_block, offset, len);
  this.mac.init(param);
  this.seqNo = 0;
}

代码示例来源:origin: redfish64/TinyTravelTracker

public void init( String pwStr, int keySize, byte[] salt, byte[] pwVerification ) throws ZipException {
  byte[] pwBytes = pwStr.getBytes();
  
  super.saltBytes = salt;
  PBEParametersGenerator generator = new PKCS5S2ParametersGenerator();
  generator.init( pwBytes, salt, ITERATION_COUNT );
  cipherParameters = generator.generateDerivedParameters(KEY_SIZE_BIT*2 + 16);
  byte[] keyBytes = ((KeyParameter)cipherParameters).getKey();
  this.cryptoKeyBytes = new byte[ KEY_SIZE_BYTE ];
  System.arraycopy( keyBytes, 0, cryptoKeyBytes, 0, KEY_SIZE_BYTE );
  this.authenticationCodeBytes = new byte[ KEY_SIZE_BYTE ];
  System.arraycopy( keyBytes, KEY_SIZE_BYTE, authenticationCodeBytes, 0, KEY_SIZE_BYTE );
  // based on SALT + PASSWORD (password is probably correct)
  this.pwVerificationBytes = new byte[ 2 ];
  System.arraycopy( keyBytes, KEY_SIZE_BYTE*2, this.pwVerificationBytes, 0, 2 );
  if( !ByteArrayHelper.isEqual( this.pwVerificationBytes, pwVerification ) ) {
    throw new ZipException("wrong password - " + ByteArrayHelper.toString(this.pwVerificationBytes) + "/ " + ByteArrayHelper.toString(pwVerification));
  }
  // create the first 16 bytes of the key sequence again (using pw+salt)
  generator.init( pwBytes, salt, ITERATION_COUNT );
  cipherParameters = generator.generateDerivedParameters(KEY_SIZE_BIT);
  // checksum added to the end of the encrypted data, update on each encryption call
  this.mac = new HMac( new SHA1Digest() );
  mac.init( new KeyParameter(authenticationCodeBytes) );
  this.aesCipher = new SICBlockCipher(new AESEngine());
  this.blockSize = aesCipher.getBlockSize();
  // incremented on each 16 byte block and used as encryption NONCE (ivBytes)
  nonce = 1;
}

代码示例来源:origin: org.bouncycastle/bcprov-debug-jdk15on

public PKCS5S2ParametersGenerator(Digest digest)
{
  hMac = new HMac(digest);
  state = new byte[hMac.getMacSize()];
}

相关文章