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


Java Mac.doFinal方法代码示例

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


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

示例1: doSign

import javax.crypto.Mac; //导入方法依赖的package包/类
private static String doSign(String baseString) {
    String apiSecret = Constants.FanFou.CONSUMER_SECRET;
    String tokenSecret = Constants.FanFou.OAUTH_TOKENSECRET;
    String keyString = OAuthEncoder.encode(apiSecret) + '&';
    if (tokenSecret != null) {
        keyString += OAuthEncoder.encode(tokenSecret);
    }

    try {
        SecretKeySpec key = new SecretKeySpec(keyString.getBytes(CHARSET), HMAC_SHA1);
        Mac mac = Mac.getInstance(HMAC_SHA1);
        mac.init(key);
        byte[] bytes = mac.doFinal(baseString.getBytes(CHARSET));
        return bytesToBase64String(bytes).replace(CARRIAGE_RETURN, EMPTY_STRING);
    } catch (Exception e) {
        e.printStackTrace();
        Logger.e("doSign error:" + e.getMessage());
        throw new RuntimeException(e);
    }
}
 
开发者ID:betroy,项目名称:xifan,代码行数:21,代码来源:XAuthUtils.java

示例2: calculateRFC2104HMAC

import javax.crypto.Mac; //导入方法依赖的package包/类
/**
 * Computes RFC 2104-compliant HMAC signature.
 * * @param data
 * The data to be signed.
 * @param key
 * The signing key.
 * @return
 * The Base64-encoded RFC 2104-compliant HMAC signature.
 * @throws
 * SignatureException when signature generation fails
 */
private static String calculateRFC2104HMAC(String data, String key)
        throws SignatureException
{
    String result;
    try {

        // get an hmac_sha1 key from the raw key bytes
        SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM);

        // get an hmac_sha1 Mac instance and initialize with the signing key
        Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
        mac.init(signingKey);

        // compute the hmac on input data bytes
        byte[] rawHmac = mac.doFinal(data.getBytes());

        // base64-encode the hmac
        result = EncodeBase64(rawHmac);

    } catch (Exception e) {
        throw new SignatureException("Failed to generate HMAC : " + e.getMessage());
    }
    return result;
}
 
开发者ID:aysenurbilgin,项目名称:cww_framework,代码行数:36,代码来源:FatParameter.java

示例3: doTest

import javax.crypto.Mac; //导入方法依赖的package包/类
private void doTest(String algo, Provider provider)
        throws NoSuchAlgorithmException, NoSuchProviderException,
        InvalidKeyException {
    System.out.println("Test " + algo);
    Mac mac;
    try {
        mac = Mac.getInstance(algo, provider);
    } catch (NoSuchAlgorithmException nsae) {
        if ("SunPKCS11-Solaris".equals(provider.getName())) {
            // depending on Solaris configuration,
            // it can support HMAC or not with Mac
            System.out.println("Expected NoSuchAlgorithmException thrown: "
                    + nsae);
            return;
        }
        throw nsae;
    }

    byte[] plain = new byte[MESSAGE_SIZE];
    for (int i = 0; i < MESSAGE_SIZE; i++) {
        plain[i] = (byte) (i % 256);
    }

    byte[] tail = new byte[plain.length - OFFSET];
    System.arraycopy(plain, OFFSET, tail, 0, tail.length);

    SecureRandom srdm = new SecureRandom();
    byte[] keyVal = new byte[KEY_SIZE];
    srdm.nextBytes(keyVal);
    SecretKeySpec keySpec = new SecretKeySpec(keyVal, "HMAC");

    mac.init(keySpec);
    byte[] result1 = mac.doFinal(plain);

    mac.reset();
    mac.update(plain[0]);
    mac.update(plain, 1, OFFSET - 1);
    byte[] result2 = mac.doFinal(tail);

    if (!java.util.Arrays.equals(result1, result2)) {
        throw new RuntimeException("result1 and result2 are not the same");
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:44,代码来源:MacSameTest.java

示例4: 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

示例5: createSign

import javax.crypto.Mac; //导入方法依赖的package包/类
private String createSign(String message) throws BitbankException {
    try {
        String algo = "HmacSHA256";
        String secret = this.apiSecret;

        SecretKeySpec sk = new SecretKeySpec(secret.getBytes(), algo);
        Mac mac = Mac.getInstance(algo);
        mac.init(sk);
        byte[] macBytes = mac.doFinal(message.getBytes());

        StringBuilder sb = new StringBuilder(2 * macBytes.length);
        for(byte b: macBytes) {
            sb.append(String.format("%02x", b&0xff) );
        }
        return sb.toString();
    } catch (Exception e) {
        throw new BitbankException(e.getMessage());
    }
}
 
开发者ID:bitbankinc,项目名称:java-bitbankcc,代码行数:20,代码来源:Bitbankcc.java

示例6: encodeHmacSHA384

import javax.crypto.Mac; //导入方法依赖的package包/类
/**
 * HmacSHA384加密
 * 
 * @param data 待加密数据
 * @param key 密钥
 * @return byte[] 消息摘要
 * @throws Exception
 */
public static byte[] encodeHmacSHA384(byte[] data, byte[] key) throws Exception {
	// 还原密钥
	SecretKey secretKey = new SecretKeySpec(key, "HmacSHA384");
	// 实例化Mac
	Mac mac = Mac.getInstance(secretKey.getAlgorithm());
	// 初始化Mac
	mac.init(secretKey);
	// 执行消息摘要
	return mac.doFinal(data);
}
 
开发者ID:guokezheng,项目名称:automat,代码行数:19,代码来源:HmacCoder.java

示例7: encodeHmacMD2

import javax.crypto.Mac; //导入方法依赖的package包/类
/**
 * HmacMD2消息摘要
 * 
 * @param data 待做消息摘要处理的数据
 * @param key 密钥
 * @throws Exception
 */
public static byte[] encodeHmacMD2(byte[] data, byte[] key) throws Exception {
	// 还原密钥
	SecretKey secretKey = new SecretKeySpec(key, "HmacMD2");
	// 实例化Mac
	Mac mac = Mac.getInstance(secretKey.getAlgorithm());
	// 初始化Mac
	mac.init(secretKey);
	// 执行消息摘要
	return mac.doFinal(data);
}
 
开发者ID:iBase4J,项目名称:iBase4J-Common,代码行数:18,代码来源:HmacCoder.java

示例8: execute

import javax.crypto.Mac; //导入方法依赖的package包/类
@Override
public void execute(IngestDocument ingestDocument) throws Exception {
    List<?> list = null;
    try {
      String in = ingestDocument.getFieldValue(field, String.class);
      list = new ArrayList<>(Arrays.asList(in));
    } catch (IllegalArgumentException e) {
      list = ingestDocument.getFieldValue(field, ArrayList.class, true);
      if (list == null)
          throw new IllegalArgumentException("field [" + field + "] is null, cannot do fingerprint.");
    }

    Mac mac = Mac.getInstance(method);
    SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(Charset.defaultCharset()), method);
    mac.init(signingKey);
    ArrayList<String> outcontent = new ArrayList<>(list.size());
    for (Object o : list) {
        String s = o.toString();
        byte[] b = mac.doFinal(s.getBytes(Charset.defaultCharset()));
        String hash = new BigInteger(1, b).toString(16);
        outcontent.add(hash);
    }
    if (outcontent.size() == 1)
      ingestDocument.setFieldValue(target_field, outcontent.get(0));
    else
      ingestDocument.setFieldValue(target_field, outcontent);
}
 
开发者ID:sektorcap,项目名称:ingest-fingerprint,代码行数:28,代码来源:FingerprintProcessor.java

示例9: verifySignature

import javax.crypto.Mac; //导入方法依赖的package包/类
public static boolean verifySignature(String payload, String signature, String secret) {
    boolean isValid;
    try {
        Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
        SecretKeySpec signingKey = new SecretKeySpec(secret.getBytes(), HMAC_SHA1_ALGORITHM);
        mac.init(signingKey);
        byte[] rawHmac = mac.doFinal(payload.getBytes());
        String expected = signature.substring(5);
        String actual = new String(encode(rawHmac));
        isValid = expected.equals(actual);
    } catch (NoSuchAlgorithmException | InvalidKeyException | IllegalStateException ex) {
        throw new RuntimeException(ex.getLocalizedMessage());
    }
    return isValid;
}
 
开发者ID:codeclou,项目名称:jenkins-github-webhook-build-trigger-plugin,代码行数:16,代码来源:GitHubWebhookUtility.java

示例10: getHmacEncodingFor

import javax.crypto.Mac; //导入方法依赖的package包/类
private String getHmacEncodingFor(
   		String signature
) throws 
   		NoSuchAlgorithmException, 
   		InvalidKeyException, UnsupportedEncodingException 
   {
    Mac hasher = Mac.getInstance(HMAC_SHA1_ALGORITHM);
    hasher.init(new SecretKeySpec(Configuration.hmacAuthPassword.getBytes(), HMAC_SHA1_ALGORITHM));

    byte[] hash = hasher.doFinal(signature.getBytes());

    String base64Encoded = Base64.getEncoder().encodeToString(hash);

    return base64Encoded;
     }
 
开发者ID:messagemedia,项目名称:messages-java-sdk,代码行数:16,代码来源:BaseController.java

示例11: doSign

import javax.crypto.Mac; //导入方法依赖的package包/类
private String doSign(String toSign, String keyString) throws Exception
    {

//        Logger.getLogger("is it signing","----------------------"+toSign);
//        Logger.getLogger("is 22222222",keyString+"");

        SecretKeySpec key = new SecretKeySpec((keyString).getBytes(UTF8), HMAC_SHA1);
        Mac mac = Mac.getInstance(HMAC_SHA1);
        mac.init(key);
        byte[] bytes = mac.doFinal(toSign.getBytes(UTF8));
        return bytesToBase64String(bytes).replace(CARRIAGE_RETURN, EMPTY_STRING);
    }
 
开发者ID:farhanhedr,项目名称:woocommerce-oauth1.0-java,代码行数:13,代码来源:HMACSha1SignatureService.java

示例12: hmacSha1

import javax.crypto.Mac; //导入方法依赖的package包/类
/**
 *
 * @param data data to hash
 * @param key key used
 * @return created hmacsha1
 */
public static byte[] hmacSha1(byte[] data, byte[] key) {
    try {
        // Get an hmac_sha1 key from the raw key bytes;
        SecretKeySpec signingKey = new SecretKeySpec(key, "HmacSHA1");
        // Get an hmac_sha1 Mac instance and initialize with the signing key
        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(signingKey);
        // Compute the hmac on input data bytes
        return mac.doFinal(data);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
开发者ID:IIlllII,项目名称:bitbreeds-webrtc,代码行数:20,代码来源:SignalUtil.java

示例13: encodeHmacSHA384

import javax.crypto.Mac; //导入方法依赖的package包/类
/**
 * HmacSHA384加密
 * 
 * @param data 待加密数据
 * @param key 密钥
 * @throws Exception
 */
public static byte[] encodeHmacSHA384(byte[] data, byte[] key) throws Exception {
	// 还原密钥
	SecretKey secretKey = new SecretKeySpec(key, "HmacSHA384");
	// 实例化Mac
	Mac mac = Mac.getInstance(secretKey.getAlgorithm());
	// 初始化Mac
	mac.init(secretKey);
	// 执行消息摘要
	return mac.doFinal(data);
}
 
开发者ID:iBase4J,项目名称:iBase4J-Common,代码行数:18,代码来源:HmacCoder.java

示例14: createPassword

import javax.crypto.Mac; //导入方法依赖的package包/类
/**
 * Compute HMAC of the identifier using the secret key and return the 
 * output as password
 * @param identifier the bytes of the identifier
 * @param key the secret key
 * @return the bytes of the generated password
 */
protected static byte[] createPassword(byte[] identifier, 
                                       SecretKey key) {
  Mac mac = threadLocalMac.get();
  try {
    mac.init(key);
  } catch (InvalidKeyException ike) {
    throw new IllegalArgumentException("Invalid key to HMAC computation", 
                                       ike);
  }
  return mac.doFinal(identifier);
}
 
开发者ID:naver,项目名称:hadoop,代码行数:19,代码来源:SecretManager.java

示例15: doTest

import javax.crypto.Mac; //导入方法依赖的package包/类
/**
 * Uses a random generator to initialize a message, instantiate a Mac object
 * according to the given PBMAC1 algorithm, initialize the object with a
 * SecretKey derived using PBKDF2 algorithm (see PKCS #5 v21, chapter 7.1),
 * feed the message into the Mac object all at once and get the output MAC
 * as result1. Reset the Mac object, chop the message into three pieces,
 * feed into the Mac object sequentially, and get the output MAC as result2.
 * Finally, compare result1 and result2 and see if they are the same.
 *
 * @param theMacAlgo PBMAC algorithm to test
 * @param thePBKDF2Algo PBKDF2 algorithm to test
 * @return true - the test is passed; false - otherwise.
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeyException
 * @throws InvalidKeySpecException
 */
protected boolean doTest(String theMacAlgo, String thePBKDF2Algo)
        throws NoSuchAlgorithmException, InvalidKeyException,
        InvalidKeySpecException {
    int OFFSET = 5;

    // Some message for which a MAC result will be calculated
    byte[] plain = new byte[25];
    new SecureRandom().nextBytes(plain);

    // Form tail - is one of the three pieces
    byte[] tail = new byte[plain.length - OFFSET];
    System.arraycopy(plain, OFFSET, tail, 0, tail.length);

    // Obtain a SecretKey using PBKDF2
    SecretKey key = getSecretKey(thePBKDF2Algo);

    // Instantiate Mac object and init it with a SecretKey and calc result1
    Mac theMac = Mac.getInstance(theMacAlgo);
    theMac.init(key);
    byte[] result1 = theMac.doFinal(plain);

    if (!isMacLengthExpected(theMacAlgo, result1.length)) {
        return false;
    }

    // Reset Mac and calculate result2
    theMac.reset();
    theMac.update(plain[0]);
    theMac.update(plain, 1, OFFSET - 1);
    byte[] result2 = theMac.doFinal(tail);

    // Return result
    if (!java.util.Arrays.equals(result1, result2)) {
        System.out.println("result1 and result2 are not the same:");
        System.out.println("result1: " + dumpByteArray(result1));
        System.out.println("result2: " + dumpByteArray(result2));
        return false;
    } else {
        System.out.println("Resulted MAC with update and doFinal is same");
    }

    return true;
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:60,代码来源:PBMacDoFinalVsUpdate.java


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