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


Java Mac类代码示例

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


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

示例1: sign

import javax.crypto.Mac; //导入依赖的package包/类
/**
 * @brief 签名
 * @author [email protected]
 * @date 2014-08-13 21:07:27
 *
 * @param signStr 被加密串
 * @param secret 加密密钥
 *
 * @return
 */
public static String sign(String signStr, String secret) 
		throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException 
{

    String sig = null;
    Mac mac = Mac.getInstance(HMAC_ALGORITHM);
    SecretKeySpec secretKey = new SecretKeySpec(secret.getBytes(CONTENT_CHARSET), mac.getAlgorithm());

    mac.init(secretKey);
    byte[] hash = mac.doFinal(signStr.getBytes(CONTENT_CHARSET));

    // base64
    //sig = new String(new BASE64Encoder().encode(hash).getBytes());
    //sig = new String(Base64.encodeBase64(hash));
    sig = new String(Base64.encode(hash));

    return sig;
}
 
开发者ID:BennyThink,项目名称:qcloudClient,代码行数:29,代码来源:Sign.java

示例2: run

import javax.crypto.Mac; //导入依赖的package包/类
@Override
public void run(Provider p) throws Exception {
    Mac mac = Mac.getInstance(alg, p);
    SecretKey keySpec = new SecretKeySpec(key, alg);
    mac.init(keySpec);
    mac.update(input);
    byte[] macv = mac.doFinal();
    if (Arrays.equals(macvalue, macv) == false) {
        System.out.println("Mac test for " + alg + " failed:");
        if (input.length < 256) {
            System.out.println("input:       "
                    + PKCS11Test.toString(input));
        }
        System.out.println("key:        " + PKCS11Test.toString(key));
        System.out.println("macvalue:   "
                + PKCS11Test.toString(macvalue));
        System.out.println("calculated: " + PKCS11Test.toString(macv));
        throw new Exception("Mac test for " + alg + " failed");
    }
    System.out.println("passed: " + alg);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:22,代码来源:MacKAT.java

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

示例4: hkdfExpand

import javax.crypto.Mac; //导入依赖的package包/类
public static byte[] hkdfExpand(byte[] prk, byte[] info, int len) throws NoSuchAlgorithmException, InvalidKeyException {
  Mac hmacHasher = makeHMACHasher(prk);

  byte[] T  = {};
  byte[] Tn = {};

  int iterations = (int) Math.ceil(((double)len) / (BLOCKSIZE));
  for (int i = 0; i < iterations; i++) {
    Tn = digestBytes(Utils.concatAll(Tn, info, Utils.hex2Byte(Integer.toHexString(i + 1))),
                     hmacHasher);
    T = Utils.concatAll(T, Tn);
  }

  byte[] result = new byte[len];
  System.arraycopy(T, 0, result, 0, len);
  return result;
}
 
开发者ID:mozilla-mobile,项目名称:FirefoxData-android,代码行数:18,代码来源:HKDF.java

示例5: computeSignature

import javax.crypto.Mac; //导入依赖的package包/类
private byte[] computeSignature(String baseString)
        throws GeneralSecurityException, UnsupportedEncodingException {
    SecretKey key = null;
    synchronized (this) {
        if (this.key == null) {
            String keyString = OAuth.percentEncode(getConsumerSecret())
                    + '&' + OAuth.percentEncode(getTokenSecret());
            byte[] keyBytes = keyString.getBytes(ENCODING);
            this.key = new SecretKeySpec(keyBytes, MAC_NAME);
        }
        key = this.key;
    }
    Mac mac = Mac.getInstance(MAC_NAME);
    mac.init(key);
    byte[] text = baseString.getBytes(ENCODING);
    return mac.doFinal(text);
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:18,代码来源:HMAC_SHA1.java

示例6: hmacTemplate

import javax.crypto.Mac; //导入依赖的package包/类
/**
 * Hmac加密模板
 *
 * @param data      数据
 * @param key       秘钥
 * @param algorithm 加密算法
 * @return 密文字节数组
 */
private static byte[] hmacTemplate(byte[] data, byte[] key, String algorithm)
{
    if(data == null || data.length == 0 || key == null || key.length == 0) return null;
    try
    {
        SecretKeySpec secretKey = new SecretKeySpec(key, algorithm);
        Mac mac = Mac.getInstance(algorithm);
        mac.init(secretKey);
        return mac.doFinal(data);
    } catch(InvalidKeyException | NoSuchAlgorithmException e)
    {
        e.printStackTrace();
        return null;
    }
}
 
开发者ID:Ayvytr,项目名称:EasyAndroid,代码行数:24,代码来源:EncryptTool.java

示例7: computeHash

import javax.crypto.Mac; //导入依赖的package包/类
/**
 * Computes the hmacSha256 hash of the data using the key given
 * @param keyBytes The key to use to calculate the hash
 * @param dataBytes The data to use to calculate the hash
 * @return The hmacSha256 hash of the data using the key
 */
private static byte[] computeHash(byte[] keyBytes, byte[] dataBytes)
{
	try
	{
		Mac hmacSha256 = Mac.getInstance("HmacSHA256");
		SecretKeySpec key = new SecretKeySpec(keyBytes, "HmacSHA256");
		hmacSha256.init(key);
		byte[] b = hmacSha256.doFinal(dataBytes);

		return b;
	}
	catch( Exception e )
	{
		e.printStackTrace();
	}

	return null;
}
 
开发者ID:equella,项目名称:Equella,代码行数:25,代码来源:D2LSigner.java

示例8: doTest

import javax.crypto.Mac; //导入依赖的package包/类
/**
 * Tests Mac.update(ByteBuffer input) method. Three test cases are
 * performed: - large ByteBuffer test case to test if the update() method
 * process a large ByteBuffer correctly; - empty ByteBuffer test case to
 * test if the update() method process an empty ByteBuffer correctly; - NULL
 * ByteBuffer test case to test if the update() method throws expected
 * IllegalArgumentException exception.
 *
 * @param theMacAlgo PBMAC algorithm to test
 * @param thePBKDF2Algo PBKDF2 algorithm to test
 * @return true - test passed; false - otherwise.
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeyException
 * @throws InvalidKeySpecException
 * @see javax.crypto.Mac
 */
protected boolean doTest(String theMacAlgo, String thePBKDF2Algo)
        throws NoSuchAlgorithmException, InvalidKeyException,
        InvalidKeySpecException {
    // obtain a SecretKey using PBKDF2
    SecretKey key = getSecretKey(thePBKDF2Algo);

    // Instantiate Mac object and init it with a SecretKey
    Mac theMac = Mac.getInstance(theMacAlgo);
    theMac.init(key);

    // Do large ByteBuffer test case
    if (!largeByteBufferTest(theMac)) {
        System.out.println("Large ByteBuffer test case failed.");
        return false;
    }

    // Do empty ByteBuffer test case
    if (!emptyByteBufferTest(theMac)) {
        System.out.println("Empty ByteBuffer test case failed.");
        return false;
    }

    // Do null ByteBuffer test case
    if (!nullByteBufferTest(theMac)) {
        System.out.println("NULL ByteBuffer test case failed.");
        return false;
    }

    return true;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:47,代码来源:PBMacBuffer.java

示例9: calculateHash

import javax.crypto.Mac; //导入依赖的package包/类
static String calculateHash(String secret, String url, String algorithm) throws InvalidKeyException, NoSuchAlgorithmException {
    Mac shaHmac = Mac.getInstance(algorithm);
    SecretKeySpec secretKey = new SecretKeySpec(secret.getBytes(), algorithm);
    shaHmac.init(secretKey);
    byte[] hash = shaHmac.doFinal(url.getBytes());
    return Hex.encodeHexString(hash);
}
 
开发者ID:CCob,项目名称:bittrex4j,代码行数:8,代码来源:EncryptionUtility.java

示例10: hmacTemplate

import javax.crypto.Mac; //导入依赖的package包/类
/**
 * Hmac加密模板
 *
 * @param data      数据
 * @param key       秘钥
 * @param algorithm 加密算法
 * @return 密文字节数组
 */
private static byte[] hmacTemplate(byte[] data, byte[] key, String algorithm) {
    if (data == null || data.length == 0 || key == null || key.length == 0) return null;
    try {
        SecretKeySpec secretKey = new SecretKeySpec(key, algorithm);
        Mac mac = Mac.getInstance(algorithm);
        mac.init(secretKey);
        return mac.doFinal(data);
    } catch (InvalidKeyException | NoSuchAlgorithmException e) {
        e.printStackTrace();
        return null;
    }
}
 
开发者ID:tututututututu,项目名称:BaseCore,代码行数:21,代码来源:EncryptUtils.java

示例11: encodeHmacSHA512

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

示例12: doHMACSHA256

import javax.crypto.Mac; //导入依赖的package包/类
private static String doHMACSHA256(String part1AndPart2, String secretKey) throws Exception {
    Mac mac = Mac.getInstance("HmacSHA256");
    mac.init(new SecretKeySpec(secretKey.getBytes(), "HmacSHA256"));

    byte[] hashBytes = mac.doFinal(part1AndPart2.getBytes());
    String hash = doBASE64(hashBytes);
    return hash;
}
 
开发者ID:rahmanusta,项目名称:JwtDemo,代码行数:9,代码来源:JwtApp.java

示例13: 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:mumucommon,项目名称:mumu-core,代码行数:19,代码来源:HmacCoder.java

示例14: testMultipleUpdates

import javax.crypto.Mac; //导入依赖的package包/类
public void testMultipleUpdates() throws Exception {
  Mac mac = Mac.getInstance("HmacSHA1");
  mac.init(SHA1_KEY);
  mac.update("hello".getBytes(UTF_8));
  mac.update("world".getBytes(UTF_8));

  assertEquals(
      HashCode.fromBytes(mac.doFinal()),
      Hashing.hmacSha1(SHA1_KEY).newHasher()
          .putString("hello", UTF_8)
          .putString("world", UTF_8)
          .hash());
}
 
开发者ID:paul-hammant,项目名称:googles-monorepo-demo,代码行数:14,代码来源:MacHashFunctionTest.java

示例15: getSignature

import javax.crypto.Mac; //导入依赖的package包/类
private String getSignature(String key, String text) {
    try {
        SecretKey secretKey = new SecretKeySpec(key.getBytes(ENC), HMAC_SHA1);
        Mac mac = Mac.getInstance(HMAC_SHA1);
        mac.init(secretKey);
        byte[] result = mac.doFinal(text.getBytes(ENC));

        return Base64.encodeToString(result, Base64.NO_WRAP);
    } catch (UnsupportedEncodingException | NoSuchAlgorithmException | InvalidKeyException e) {
        return "";
    }
}
 
开发者ID:mingdroid,项目名称:tumbviewer,代码行数:13,代码来源:OAuth1SigningHelper.java


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