當前位置: 首頁>>代碼示例>>Java>>正文


Java Mac.getInstance方法代碼示例

本文整理匯總了Java中javax.crypto.Mac.getInstance方法的典型用法代碼示例。如果您正苦於以下問題:Java Mac.getInstance方法的具體用法?Java Mac.getInstance怎麽用?Java Mac.getInstance使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在javax.crypto.Mac的用法示例。


在下文中一共展示了Mac.getInstance方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: hashCredentials

import javax.crypto.Mac; //導入方法依賴的package包/類
/**
 * Hash user name and password according to the algorithm required by the Miniserver
 *
 * @param hashKeyHex
 *            string with hash key received from the Miniserver in hex characters
 * @return
 *         hashed credentials to send to the Miniserver for authentication
 */
private String hashCredentials(String hashKeyHex) {
    if (user == null || password == null || hashKeyHex == null) {
        return null;
    }
    try {
        byte[] hashKeyBytes = Hex.decodeHex(hashKeyHex.toCharArray());
        SecretKeySpec signKey = new SecretKeySpec(hashKeyBytes, "HmacSHA1");
        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(signKey);
        String data = user + ":" + password;
        byte[] rawData = mac.doFinal(data.getBytes());
        byte[] hexData = new Hex().encode(rawData);
        return new String(hexData, "UTF-8");
    } catch (DecoderException | NoSuchAlgorithmException | InvalidKeyException
            | UnsupportedEncodingException e) {
        logger.debug("[{}] Error encrypting credentials: {}", debugId, e.getMessage());
        return null;
    }
}
 
開發者ID:ppieczul,項目名稱:org.openhab.binding.loxone,代碼行數:28,代碼來源:LxWsClient.java

示例2: computeAccessSignature

import javax.crypto.Mac; //導入方法依賴的package包/類
private String computeAccessSignature(String timestamp, HttpMethod method, String urlTxt, ByteBuffer body)
        throws GeneralSecurityException {
    if (conn == null) {
        throw new IllegalStateException("cannot generate exchange request post-disconnect()");
    }

    String prehash = timestamp + method.name() + urlTxt;

    Mac mac = Mac.getInstance("HmacSHA256");
    mac.init(signingKey);
    mac.update(prehash.getBytes());
    if (body != null) {
        mac.update(body);
    }
    return new String(Base64.encodeBase64(mac.doFinal()));
}
 
開發者ID:cloudwall,項目名稱:libcwfincore,代碼行數:17,代碼來源:GdaxExchangeSession.java

示例3: if

import javax.crypto.Mac; //導入方法依賴的package包/類
private byte[] b0449щ0449щ04490449(String str, byte[] bArr) throws Exception {
    try {
        String str2 = "HmacSHA256";
        if (((bХ0425ХХХХ + bХХ0425ХХХ) * bХ0425ХХХХ) % bХ042504250425ХХ != b04250425ХХХХ) {
            bХ0425ХХХХ = 57;
            b04250425ХХХХ = bХ04250425ХХХ();
        }
        try {
            Mac instance = Mac.getInstance(str2);
            instance.init(new SecretKeySpec(bArr, str2));
            return instance.doFinal(str.getBytes(UrlUtils.UTF8));
        } catch (Exception e) {
            throw e;
        }
    } catch (Exception e2) {
        throw e2;
    }
}
 
開發者ID:JackChan1999,項目名稱:letv,代碼行數:19,代碼來源:crrcrc.java

示例4: hmacSHA256

import javax.crypto.Mac; //導入方法依賴的package包/類
static byte[] hmacSHA256(String data, byte[] key) {
  try {
    String algorithm = "HmacSHA256";
    Mac mac = Mac.getInstance(algorithm);
    mac.init(new SecretKeySpec(key, algorithm));
    return mac.doFinal(data.getBytes(UTF_8));
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}
 
開發者ID:wenwu315,項目名稱:XXXX,代碼行數:11,代碼來源:AWSSignatureVersion4.java

示例5: encodeHmacSHA256

import javax.crypto.Mac; //導入方法依賴的package包/類
/**
 * HmacSHA256加密
 * 
 * @param data 待加密數據
 * @param key 密鑰
 * @return byte[] 消息摘要
 * @throws Exception
 */
public static byte[] encodeHmacSHA256(byte[] data, byte[] key) throws Exception {
	// 還原密鑰
	SecretKey secretKey = new SecretKeySpec(key, "HmacSHA256");
	// 實例化Mac
	Mac mac = Mac.getInstance(secretKey.getAlgorithm());
	// 初始化Mac
	mac.init(secretKey);
	// 執行消息摘要
	return mac.doFinal(data);
}
 
開發者ID:babymm,項目名稱:mumu,代碼行數:19,代碼來源:HmacCoder.java

示例6: generateHash

import javax.crypto.Mac; //導入方法依賴的package包/類
/**
 * Generate a Hmac SHA-256 hash
 * @param key key
 * @param string value
 * @return hashed
 */
public static String generateHash(String key, String string) {
    SecretKeySpec object = new SecretKeySpec(key.getBytes(), "HmacSHA256");
    try {
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init((Key) object);
        byte[] byteArray = mac.doFinal(string.getBytes("UTF-8"));
        return new String(new Hex().encode(byteArray), "ISO-8859-1");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
 
開發者ID:brunocvcunha,項目名稱:instagram4j,代碼行數:19,代碼來源:InstagramHashUtil.java

示例7: calculateSignature

import javax.crypto.Mac; //導入方法依賴的package包/類
public static String calculateSignature(int nonce, long timestamp) {
    String data = composeParametersToString(nonce, timestamp);

    try {
        SecretKeySpec signingKey = new SecretKeySpec(ApiConstants.AUTH_SECRET.getBytes(),
                HMAC_SHA1_ALGORITHM);
        Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
        mac.init(signingKey);
        return toHexString(mac.doFinal(data.getBytes()));

    } catch(NoSuchAlgorithmException | InvalidKeyException ex){
        Logger.logExceptionToFabric(ex);
    }
    return null;
}
 
開發者ID:ukevgen,項目名稱:BizareChat,代碼行數:16,代碼來源:HmacSha1Signature.java

示例8: calculateMac

import javax.crypto.Mac; //導入方法依賴的package包/類
private byte[] calculateMac(char[] passwd, byte[] data)
    throws IOException
{
    byte[] mData = null;
    String algName = "SHA1";

    try {
        // Generate a random salt.
        byte[] salt = getSalt();

        // generate MAC (MAC key is generated within JCE)
        Mac m = Mac.getInstance("HmacPBESHA1");
        PBEParameterSpec params =
                    new PBEParameterSpec(salt, iterationCount);
        SecretKey key = getPBEKey(passwd);
        m.init(key, params);
        m.update(data);
        byte[] macResult = m.doFinal();

        // encode as MacData
        MacData macData = new MacData(algName, macResult, salt,
                                            iterationCount);
        DerOutputStream bytes = new DerOutputStream();
        bytes.write(macData.getEncoded());
        mData = bytes.toByteArray();
    } catch (Exception e) {
        throw new IOException("calculateMac failed: " + e, e);
    }
    return mData;
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:31,代碼來源:PKCS12KeyStore.java

示例9: encodeHmacSHA

import javax.crypto.Mac; //導入方法依賴的package包/類
/**
 * HmacSHA1加密
 * 
 * @param data 待加密數據
 * @param key 密鑰
 * @return byte[] 消息摘要
 * @throws Exception
 */
public static byte[] encodeHmacSHA(byte[] data, byte[] key) throws Exception {
	// 還原密鑰
	SecretKey secretKey = new SecretKeySpec(key, "HMacTiger");
	// 實例化Mac SslMacMD5
	Mac mac = Mac.getInstance("SslMacMD5");// secretKey.getAlgorithm());
	// 初始化Mac
	mac.init(secretKey);
	// 執行消息摘要
	return mac.doFinal(data);
}
 
開發者ID:tb544731152,項目名稱:iBase4J,代碼行數:19,代碼來源:HmacCoder.java

示例10: encodeHmacMD4

import javax.crypto.Mac; //導入方法依賴的package包/類
/**
 * HmacMD4消息摘要
 * 
 * @param data 待做消息摘要處理的數據
 * @param byte[] 密鑰
 * @return byte[] 消息摘要
 * @throws Exception
 */
public static byte[] encodeHmacMD4(byte[] data, byte[] key) throws Exception {
	// 還原密鑰
	SecretKey secretKey = new SecretKeySpec(key, "HmacMD4");
	// 實例化Mac
	Mac mac = Mac.getInstance(secretKey.getAlgorithm());
	// 初始化Mac
	mac.init(secretKey);
	// 執行消息摘要
	return mac.doFinal(data);
}
 
開發者ID:youngMen1,項目名稱:JAVA-,代碼行數:19,代碼來源:HmacCoder.java

示例11: MasterCipher

import javax.crypto.Mac; //導入方法依賴的package包/類
public MasterCipher(MasterSecret masterSecret) {
  try {
    this.masterSecret = masterSecret;		
    this.encryptingCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    this.decryptingCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    this.hmac             = Mac.getInstance("HmacSHA1");
  } catch (NoSuchPaddingException | NoSuchAlgorithmException nspe) {
    throw new AssertionError(nspe);
  }
}
 
開發者ID:CableIM,項目名稱:Cable-Android,代碼行數:11,代碼來源:MasterCipher.java

示例12: doTest

import javax.crypto.Mac; //導入方法依賴的package包/類
@Override
public void doTest(String alg) throws NoSuchAlgorithmException,
        InvalidKeyException, NoSuchProviderException {
    SecretKey key = Utils.getSecretKeySpec();

    // instantiate Mac object and init it with a SecretKey
    Mac mac = Mac.getInstance(alg, "SunJCE");
    mac.init(key);

    // prepare buffer
    byte[] data = new byte[BUFFER_SIZE];
    for (int i = 0; i < BUFFER_SIZE; i++) {
        data[i] = (byte) (i % 256);
    }

    ByteBuffer buf = ByteBuffer.wrap(data);
    int limitBefore = buf.limit();

    mac.update(buf);
    mac.doFinal();

    int limitAfter = buf.limit();
    int positonAfter = buf.position();

    if (limitAfter != limitBefore) {
        System.out.println("limit after = " + limitAfter);
        System.out.println("limit before = " + limitBefore);
        throw new RuntimeException("Test failed: "
                + "limit of buffer has been chenged.");
    }

    if (positonAfter != limitAfter) {
        System.out.println("position after = " + positonAfter);
        System.out.println("limit after = " + limitAfter);
        throw new RuntimeException("Test failed: "
                + "position of buffer isn't equal to its limit");
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:39,代碼來源:LargeByteBufferTest.java

示例13: sign

import javax.crypto.Mac; //導入方法依賴的package包/類
private String sign(
    String httpVerb,
    String contentMD5,
    String contentType,
    String date,
    String resource,
    Map<String, String> metas) {

  StringBuilder stringToSign =
      new StringBuilder(
          httpVerb
              + "\n"
              + CharMatcher.whitespace().trimFrom(contentMD5)
              + "\n"
              + CharMatcher.whitespace().trimFrom(contentType)
              + "\n"
              + date
              + "\n");
  if (metas != null) {
    for (Map.Entry<String, String> entity : metas.entrySet()) {
      stringToSign
          .append(CharMatcher.whitespace().trimFrom(entity.getKey()))
          .append(":")
          .append(CharMatcher.whitespace().trimFrom(entity.getValue()))
          .append("\n");
    }
  }
  stringToSign.append(resource);
  try {
    Mac mac = Mac.getInstance("HmacSHA1");
    byte[] keyBytes = secretKey.getBytes("UTF8");
    SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA1");
    mac.init(signingKey);
    byte[] signBytes = mac.doFinal(stringToSign.toString().getBytes("UTF8"));
    String signature = encodeBase64(signBytes);
    return "AWS" + " " + accessKey + ":" + signature;
  } catch (Exception e) {
    throw new RuntimeException("MAC CALC FAILED.");
  }
}
 
開發者ID:twonote,項目名稱:radosgw-admin4j,代碼行數:41,代碼來源:S3Auth.java

示例14: hmacSha1

import javax.crypto.Mac; //導入方法依賴的package包/類
/**
 * 使用HMAC-SHA1進行消息簽名, 返回字節數組,長度為20字節.
 *
 * @param input 原始輸入字符數組
 * @param key   HMAC-SHA1密鑰
 */
public static byte[] hmacSha1(byte[] input, byte[] key) {
	try {
		SecretKey secretKey = new SecretKeySpec(key, HMACSHA1);
		Mac mac = Mac.getInstance(HMACSHA1);
		mac.init(secretKey);
		return mac.doFinal(input);
	} catch (GeneralSecurityException e) {
		throw Exceptions.unchecked(e);
	}
}
 
開發者ID:funtl,項目名稱:framework,代碼行數:17,代碼來源:Cryptos.java

示例15: IntegrityHmac

import javax.crypto.Mac; //導入方法依賴的package包/類
/**
 * Method IntegrityHmac
 *
 * @throws XMLSignatureException
 */
public IntegrityHmac() throws XMLSignatureException {
    String algorithmID = JCEMapper.translateURItoJCEID(this.engineGetURI());
    if (log.isLoggable(java.util.logging.Level.FINE)) {
        log.log(java.util.logging.Level.FINE, "Created IntegrityHmacSHA1 using " + algorithmID);
    }

    try {
        this.macAlgorithm = Mac.getInstance(algorithmID);
    } catch (java.security.NoSuchAlgorithmException ex) {
        Object[] exArgs = { algorithmID, ex.getLocalizedMessage() };

        throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs);
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:20,代碼來源:IntegrityHmac.java


注:本文中的javax.crypto.Mac.getInstance方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。