当前位置: 首页>>代码示例>>Java>>正文


Java Mac.getMacLength方法代码示例

本文整理汇总了Java中javax.crypto.Mac.getMacLength方法的典型用法代码示例。如果您正苦于以下问题:Java Mac.getMacLength方法的具体用法?Java Mac.getMacLength怎么用?Java Mac.getMacLength使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.crypto.Mac的用法示例。


在下文中一共展示了Mac.getMacLength方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: verifyMacBody

import javax.crypto.Mac; //导入方法依赖的package包/类
private byte[] verifyMacBody(@NonNull Mac hmac, @NonNull byte[] encryptedAndMac) throws InvalidMessageException {
  if (encryptedAndMac.length < hmac.getMacLength()) {
    throw new InvalidMessageException("length(encrypted body + MAC) < length(MAC)");
  }

  byte[] encrypted = new byte[encryptedAndMac.length - hmac.getMacLength()];
  System.arraycopy(encryptedAndMac, 0, encrypted, 0, encrypted.length);

  byte[] remoteMac = new byte[hmac.getMacLength()];
  System.arraycopy(encryptedAndMac, encryptedAndMac.length - remoteMac.length, remoteMac, 0, remoteMac.length);

  byte[] localMac  = hmac.doFinal(encrypted);

  if (!Arrays.equals(remoteMac, localMac))
    throw new InvalidMessageException("MAC doesen't match.");

  return encrypted;
}
 
开发者ID:XecureIT,项目名称:PeSanKita-android,代码行数:19,代码来源:MasterCipher.java

示例2: verifyMacBody

import javax.crypto.Mac; //导入方法依赖的package包/类
private byte[] verifyMacBody(Mac hmac, byte[] encryptedAndMac) throws Exception {
  byte[] encrypted = new byte[encryptedAndMac.length - hmac.getMacLength()];
  System.arraycopy(encryptedAndMac, 0, encrypted, 0, encrypted.length);

  byte[] remoteMac = new byte[hmac.getMacLength()];
  System.arraycopy(encryptedAndMac, encryptedAndMac.length - remoteMac.length, remoteMac, 0,
      remoteMac.length);

  byte[] localMac = hmac.doFinal(encrypted);

  System.out.println("Message Authentication code remoteMac : " + toHexString(remoteMac));
  System.out.println("Message Authentication code localMac : " + toHexString(localMac));
  if (!Arrays.equals(remoteMac, localMac))
    throw new Exception("MAC doesen't match.");

  return encrypted;
}
 
开发者ID:ampool,项目名称:monarch,代码行数:18,代码来源:GMSEncryptJUnitTest.java

示例3: verifyMac

import javax.crypto.Mac; //导入方法依赖的package包/类
private static byte[] verifyMac(byte[] macSalt, int iterations, byte[] encryptedAndMacdData, String passphrase) throws InvalidPassphraseException, GeneralSecurityException, IOException {
  Mac hmac        = getMacForPassphrase(passphrase, macSalt, iterations);

  byte[] encryptedData = new byte[encryptedAndMacdData.length - hmac.getMacLength()];
  System.arraycopy(encryptedAndMacdData, 0, encryptedData, 0, encryptedData.length);

  byte[] givenMac      = new byte[hmac.getMacLength()];
  System.arraycopy(encryptedAndMacdData, encryptedAndMacdData.length-hmac.getMacLength(), givenMac, 0, givenMac.length);

  byte[] localMac      = hmac.doFinal(encryptedData);

  if (Arrays.equals(givenMac, localMac)) return encryptedData;
  else                                   throw new InvalidPassphraseException("MAC Error");
}
 
开发者ID:XecureIT,项目名称:PeSanKita-android,代码行数:15,代码来源:MasterSecretUtil.java

示例4: verifyMac

import javax.crypto.Mac; //导入方法依赖的package包/类
private void verifyMac(File file, Mac mac, Optional<byte[]> theirDigest)
    throws FileNotFoundException, InvalidMacException
{
  try {
    MessageDigest   digest        = MessageDigest.getInstance("SHA256");
    FileInputStream fin           = new FileInputStream(file);
    int             remainingData = Util.toIntExact(file.length()) - mac.getMacLength();
    byte[]          buffer        = new byte[4096];

    while (remainingData > 0) {
      int read = fin.read(buffer, 0, Math.min(buffer.length, remainingData));
      mac.update(buffer, 0, read);
      digest.update(buffer, 0, read);
      remainingData -= read;
    }

    byte[] ourMac   = mac.doFinal();
    byte[] theirMac = new byte[mac.getMacLength()];
    Util.readFully(fin, theirMac);

    if (!MessageDigest.isEqual(ourMac, theirMac)) {
      throw new InvalidMacException("MAC doesn't match!");
    }

    byte[] ourDigest = digest.digest(theirMac);

    if (theirDigest.isPresent() && !MessageDigest.isEqual(ourDigest, theirDigest.get())) {
      throw new InvalidMacException("Digest doesn't match!");
    }

  } catch (IOException | ArithmeticException e1) {
    throw new InvalidMacException(e1);
  } catch (NoSuchAlgorithmException e) {
    throw new AssertionError(e);
  }
}
 
开发者ID:XecureIT,项目名称:PeSanKita-lib,代码行数:37,代码来源:AttachmentCipherInputStream.java

示例5: testMacDigestSizes

import javax.crypto.Mac; //导入方法依赖的package包/类
@Test
public void testMacDigestSizes()
    throws NoSuchProviderException, NoSuchAlgorithmException {

    Mac mac;

    for (int i = 0; i < wolfJCEAlgos.length; i++) {
        mac = Mac.getInstance(wolfJCEAlgos[i], "wolfJCE");

        if (mac.getMacLength() != wolfJCEMacLengths[i])
            fail("Expected MAC length did not match, " +
                    "algo = " + wolfJCEAlgos[i]);
    }
}
 
开发者ID:wolfSSL,项目名称:wolfcrypt-jni,代码行数:15,代码来源:WolfCryptMacTest.java

示例6: hmacLength

import javax.crypto.Mac; //导入方法依赖的package包/类
private int hmacLength(byte[] skey) throws Exception {
    SecretKeySpec key = new SecretKeySpec(skey, "HmacSHA256");
    Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
    sha256_HMAC.init(key);
    return sha256_HMAC.getMacLength();
}
 
开发者ID:NordeaOSS,项目名称:authenticated-encryption,代码行数:7,代码来源:AuthenticatedEncryption.java

示例7: encrypt

import javax.crypto.Mac; //导入方法依赖的package包/类
public static byte[] encrypt(byte[] insecure, ExternalContext ctx)
{

    if (ctx == null)
    {
        throw new NullPointerException("ExternalContext ctx");
    }

    testConfiguration(ctx);
    
    SecretKey secretKey = (SecretKey) getSecret(ctx);
    String algorithm = findAlgorithm(ctx);
    String algorithmParams = findAlgorithmParams(ctx);
    byte[] iv = findInitializationVector(ctx);
    
    SecretKey macSecretKey = (SecretKey) getMacSecret(ctx);
    String macAlgorithm = findMacAlgorithm(ctx);
            
    try
    {
        // keep local to avoid threading issue
        Mac mac = Mac.getInstance(macAlgorithm);
        mac.init(macSecretKey);
        Cipher cipher = Cipher.getInstance(algorithm + "/" + algorithmParams);
        if (iv != null)
        {
            IvParameterSpec ivSpec = new IvParameterSpec(iv);
            cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec);
        }
        else
        {
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        }
        if (log.isLoggable(Level.FINE))
        {
            log.fine("encrypting w/ " + algorithm + "/" + algorithmParams);
        }
        
        //EtM Composition Approach
        int macLenght = mac.getMacLength();
        byte[] secure = new byte[cipher.getOutputSize(insecure.length)+ macLenght];
        int secureCount = cipher.doFinal(insecure,0,insecure.length,secure);
        mac.update(secure, 0, secureCount);
        mac.doFinal(secure, secureCount);
                    
        return secure;
    }
    catch (Exception e)
    {
        throw new FacesException(e);
    }
}
 
开发者ID:apache,项目名称:myfaces-trinidad,代码行数:53,代码来源:StateUtils.java

示例8: decrypt

import javax.crypto.Mac; //导入方法依赖的package包/类
public static byte[] decrypt(byte[] secure, ExternalContext ctx)
{
    if (ctx == null)
    {
        throw new NullPointerException("ExternalContext ctx");
    }

    testConfiguration(ctx);
            
    SecretKey secretKey = (SecretKey) getSecret(ctx);
    String algorithm = findAlgorithm(ctx);
    String algorithmParams = findAlgorithmParams(ctx);
    byte[] iv = findInitializationVector(ctx);
    
    SecretKey macSecretKey = (SecretKey) getMacSecret(ctx);
    String macAlgorithm = findMacAlgorithm(ctx);

    try
    {
        // keep local to avoid threading issue
        Mac mac = Mac.getInstance(macAlgorithm);
        mac.init(macSecretKey);
        Cipher cipher = Cipher.getInstance(algorithm + "/"
                + algorithmParams);
        if (iv != null)
        {
            IvParameterSpec ivSpec = new IvParameterSpec(iv);
            cipher.init(Cipher.DECRYPT_MODE, secretKey, ivSpec);
        }
        else
        {
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
        }
        if (log.isLoggable(Level.FINE))
        {
            log.fine("decrypting w/ " + algorithm + "/" + algorithmParams);
        }

        //EtM Composition Approach
        int macLenght = mac.getMacLength();
        mac.update(secure, 0, secure.length-macLenght);
        byte[] signedDigestHash = mac.doFinal();

        boolean isMacEqual = true;
        for (int i = 0; i < signedDigestHash.length; i++)
        {
            if (signedDigestHash[i] != secure[secure.length-macLenght+i])
            {
                isMacEqual = false;
                // MYFACES-2934 Must compare *ALL* bytes of the hash, 
                // otherwise a side-channel timing attack is theorically possible
                // but with a very very low probability, because the
                // comparison time is too small to be measured compared to
                // the overall request time and in real life applications,
                // there are too many uncertainties involved.
                //break;
            }
        }
        if (!isMacEqual)
        {
            throw new ViewExpiredException();
        }
        
        return cipher.doFinal(secure, 0, secure.length-macLenght);
    }
    catch (Exception e)
    {
        throw new FacesException(e);
    }
}
 
开发者ID:apache,项目名称:myfaces-trinidad,代码行数:71,代码来源:StateUtils.java

示例9: deriveKey

import javax.crypto.Mac; //导入方法依赖的package包/类
private static byte[] deriveKey(final Mac prf, final byte[] password,
        byte[] salt, int iterCount, int keyLengthInBit) {
    int keyLength = keyLengthInBit/8;
    byte[] key = new byte[keyLength];
    try {
        int hlen = prf.getMacLength();
        int intL = (keyLength + hlen - 1)/hlen; // ceiling
        int intR = keyLength - (intL - 1)*hlen; // residue
        byte[] ui = new byte[hlen];
        byte[] ti = new byte[hlen];
        // SecretKeySpec cannot be used, since password can be empty here.
        SecretKey macKey = new SecretKey() {
            private static final long serialVersionUID = 7874493593505141603L;
            @Override
            public String getAlgorithm() {
                return prf.getAlgorithm();
            }
            @Override
            public String getFormat() {
                return "RAW";
            }
            @Override
            public byte[] getEncoded() {
                return password;
            }
            @Override
            public int hashCode() {
                return Arrays.hashCode(password) * 41 +
                        prf.getAlgorithm().toLowerCase().hashCode();
            }
            @Override
            public boolean equals(Object obj) {
                if (this == obj) return true;
                if (this.getClass() != obj.getClass()) return false;
                SecretKey sk = (SecretKey)obj;
                return prf.getAlgorithm().equalsIgnoreCase(
                    sk.getAlgorithm()) &&
                    Arrays.equals(password, sk.getEncoded());
            }
        };
        prf.init(macKey);

        byte[] ibytes = new byte[4];
        for (int i = 1; i <= intL; i++) {
            prf.update(salt);
            ibytes[3] = (byte) i;
            ibytes[2] = (byte) ((i >> 8) & 0xff);
            ibytes[1] = (byte) ((i >> 16) & 0xff);
            ibytes[0] = (byte) ((i >> 24) & 0xff);
            prf.update(ibytes);
            prf.doFinal(ui, 0);
            System.arraycopy(ui, 0, ti, 0, ui.length);

            for (int j = 2; j <= iterCount; j++) {
                prf.update(ui);
                prf.doFinal(ui, 0);
                // XOR the intermediate Ui's together.
                for (int k = 0; k < ui.length; k++) {
                    ti[k] ^= ui[k];
                }
            }
            if (i == intL) {
                System.arraycopy(ti, 0, key, (i-1)*hlen, intR);
            } else {
                System.arraycopy(ti, 0, key, (i-1)*hlen, hlen);
            }
        }
    } catch (GeneralSecurityException gse) {
        throw new RuntimeException("Error deriving PBKDF2 keys");
    }
    return key;
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:73,代码来源:PBKDF2KeyImpl.java

示例10: deriveKey

import javax.crypto.Mac; //导入方法依赖的package包/类
private static byte[] deriveKey(final Mac prf, final byte[] password,
        byte[] salt, int iterCount, int keyLengthInBit) {
    int keyLength = keyLengthInBit/8;
    byte[] key = new byte[keyLength];
    try {
        int hlen = prf.getMacLength();
        int intL = (keyLength + hlen - 1)/hlen; // ceiling
        int intR = keyLength - (intL - 1)*hlen; // residue
        byte[] ui = new byte[hlen];
        byte[] ti = new byte[hlen];
        // SecretKeySpec cannot be used, since password can be empty here.
        SecretKey macKey = new SecretKey() {
            private static final long serialVersionUID = 7874493593505141603L;
            @Override
            public String getAlgorithm() {
                return prf.getAlgorithm();
            }
            @Override
            public String getFormat() {
                return "RAW";
            }
            @Override
            public byte[] getEncoded() {
                return password;
            }
            @Override
            public int hashCode() {
                return Arrays.hashCode(password) * 41 +
                  prf.getAlgorithm().toLowerCase(Locale.ENGLISH).hashCode();
            }
            @Override
            public boolean equals(Object obj) {
                if (this == obj) return true;
                if (this.getClass() != obj.getClass()) return false;
                SecretKey sk = (SecretKey)obj;
                return prf.getAlgorithm().equalsIgnoreCase(
                    sk.getAlgorithm()) &&
                    MessageDigest.isEqual(password, sk.getEncoded());
            }
        };
        prf.init(macKey);

        byte[] ibytes = new byte[4];
        for (int i = 1; i <= intL; i++) {
            prf.update(salt);
            ibytes[3] = (byte) i;
            ibytes[2] = (byte) ((i >> 8) & 0xff);
            ibytes[1] = (byte) ((i >> 16) & 0xff);
            ibytes[0] = (byte) ((i >> 24) & 0xff);
            prf.update(ibytes);
            prf.doFinal(ui, 0);
            System.arraycopy(ui, 0, ti, 0, ui.length);

            for (int j = 2; j <= iterCount; j++) {
                prf.update(ui);
                prf.doFinal(ui, 0);
                // XOR the intermediate Ui's together.
                for (int k = 0; k < ui.length; k++) {
                    ti[k] ^= ui[k];
                }
            }
            if (i == intL) {
                System.arraycopy(ti, 0, key, (i-1)*hlen, intR);
            } else {
                System.arraycopy(ti, 0, key, (i-1)*hlen, hlen);
            }
        }
    } catch (GeneralSecurityException gse) {
        throw new RuntimeException("Error deriving PBKDF2 keys");
    }
    return key;
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:73,代码来源:PBKDF2KeyImpl.java


注:本文中的javax.crypto.Mac.getMacLength方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。