Example usage for javax.crypto Mac init

List of usage examples for javax.crypto Mac init

Introduction

In this page you can find the example usage for javax.crypto Mac init.

Prototype

public final void init(Key key) throws InvalidKeyException 

Source Link

Document

Initializes this Mac object with the given key.

Usage

From source file:org.apache.ws.security.message.token.UsernameToken.java

/**
 * P_hash as defined in RFC 2246 for TLS.
 * //from w w  w. ja  v a  2 s .  c  o  m
 * @param secret is the key for the HMAC
 * @param seed the seed value to start the generation - A(0)
 * @param mac the HMAC algorithm
 * @param required number of bytes to generate
 * @return a byte array that contains a secret key
 * @throws Exception
 */
private static byte[] P_hash(byte[] secret, byte[] seed, Mac mac, int required) throws Exception {
    byte[] out = new byte[required];
    int offset = 0, tocpy;
    byte[] A, tmp;
    //
    // A(0) is the seed
    //
    A = seed;
    SecretKeySpec key = new SecretKeySpec(secret, "HMACSHA1");
    mac.init(key);
    while (required > 0) {
        mac.update(A);
        A = mac.doFinal();
        mac.update(A);
        mac.update(seed);
        tmp = mac.doFinal();
        tocpy = min(required, tmp.length);
        System.arraycopy(tmp, 0, out, offset, tocpy);
        offset += tocpy;
        required -= tocpy;
    }
    return out;
}

From source file:com.javaps.springboot.LicenseController.java

@RequestMapping(value = "/public/license", produces = "text/plain", method = RequestMethod.GET)
public String licenseIssue(@RequestParam(value = "ip") String clientIp) throws Exception {
    SecretKeySpec signingKey = new SecretKeySpec(licenseSecretKey.getBytes(), "HmacSHA1");
    Mac mac = Mac.getInstance("HmacSHA1");
    mac.init(signingKey);

    byte[] rawHmac = mac.doFinal(clientIp.getBytes());
    return Base64.encodeBase64String(rawHmac);
}

From source file:com.javaps.springboot.LicenseController.java

@RequestMapping(value = "/public/license", produces = "text/plain", method = RequestMethod.POST)
public String licenseValidate(HttpServletRequest req, @RequestBody String license) throws Exception {
    String clientIp = req.getHeader("X-Forwarded-For"); //nginx???IP
    if (clientIp == null)
        clientIp = req.getRemoteAddr(); //?????
    //System.out.println("clientIp="+clientIp);
    SecretKeySpec signingKey = new SecretKeySpec(licenseSecretKey.getBytes(), "HmacSHA1");
    Mac mac = Mac.getInstance("HmacSHA1");
    mac.init(signingKey);

    byte[] rawHmac = mac.doFinal(clientIp.getBytes());
    //System.out.println("license should be:"+Base64.encodeBase64String(rawHmac));
    if (!license.equals(Base64.encodeBase64String(rawHmac)))
        throw new Exception();

    return "OK";
}

From source file:com.k42b3.neodym.oauth.HMACSHA1.java

public String build(String baseString, String consumerSecret, String tokenSecret) throws Exception {
    String key = Oauth.urlEncode(consumerSecret) + "&" + Oauth.urlEncode(tokenSecret);

    Charset charset = Charset.defaultCharset();

    SecretKey sk = new SecretKeySpec(key.getBytes(charset), "HmacSHA1");

    Mac mac = Mac.getInstance("HmacSHA1");

    mac.init(sk);

    byte[] result = mac.doFinal(baseString.getBytes(charset));

    return Base64.encodeBase64String(result);
}

From source file:oauth.signpost.signature.HmacSha1MessageSigner.java

@Override
public String sign(HttpRequest request, Map<String, String> oauthParameters)
        throws OAuthMessageSignerException {
    try {/*from   w w  w.  ja  v a2 s .c  o m*/
        String keyString = OAuth.percentEncode(getConsumerSecret()) + '&'
                + OAuth.percentEncode(getTokenSecret());
        byte[] keyBytes = keyString.getBytes(OAuth.ENCODING);

        SecretKey key = new SecretKeySpec(keyBytes, MAC_NAME);
        Mac mac = Mac.getInstance(MAC_NAME);
        mac.init(key);

        String sbs = computeSignatureBaseString(request, oauthParameters);
        byte[] text = sbs.getBytes(OAuth.ENCODING);

        return base64Encode(mac.doFinal(text));
    } catch (GeneralSecurityException e) {
        throw new OAuthMessageSignerException(e);
    } catch (UnsupportedEncodingException e) {
        throw new OAuthMessageSignerException(e);
    }
}

From source file:spring.travel.site.auth.Signer.java

public String sign(String data) throws AuthException {
    try {//w ww.j a va  2s. co m
        SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), HMAC_SHA1_ALGORITHM);
        Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
        mac.init(signingKey);
        byte[] raw = mac.doFinal(data.getBytes(StandardCharsets.UTF_8));
        return toHex(raw);
    } catch (InvalidKeyException | NoSuchAlgorithmException e) {
        throw new AuthException("Failed signing data", e);
    }
}

From source file:org.esupportail.papercut.services.HashService.java

public String getHMac(String input) {
    try {/*  w w w  .j a v a2s.  co m*/
        Mac mac = Mac.getInstance("HmacSHA512");
        mac.init(secretKey);
        final byte[] macData = mac.doFinal(input.getBytes());
        byte[] hex = new Hex().encode(macData);
        String hmac = new String(hex, "ISO-8859-1").toUpperCase();
        log.debug(input);
        log.debug(hmac);
        return hmac;
    } catch (Exception e) {
        log.error("Error during encoding data ...");
        throw new RuntimeException(e);
    }
}

From source file:org.mule.module.fws.api.PortProvider.java

private String sign(String data, String key) throws SignatureException {
    byte[] signature;
    try {// w ww .ja  v  a2 s .  c  om
        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(new SecretKeySpec(key.getBytes(), "HmacSHA1"));
        signature = Base64.encodeBase64(mac.doFinal(data.getBytes("UTF-8")));
    } catch (Exception e) {
        throw new SignatureException("Failed to generate signature: " + e.getMessage(), e);
    }
    return new String(signature);
}

From source file:com.twosigma.beakerx.security.HashedMessageAuthenticationCode.java

public String signBytes(List<byte[]> msg) {
    try {/* w  w  w .  j a  va  2  s. co m*/
        final Mac mac = Mac.getInstance(TYPE);
        mac.init(spec);
        msg.forEach(it -> mac.update(it));
        byte[] digest = mac.doFinal();
        return toHex(digest);
    } catch (InvalidKeyException e) {
        throw new RuntimeException(INVALID_HMAC_EXCEPTION, e);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.moha.demo.utils.Hashsalt.java

public String encrypt(String password) {
    String algorithm = EnvUtils.getProperty("algorithm");
    String keyString = EnvUtils.getProperty("keyString");
    SecretKey key = new SecretKeySpec(keyString.getBytes(), algorithm);

    try {//from   w w  w .jav a 2s .  c  om
        Mac m = Mac.getInstance(algorithm);
        m.init(key);
        m.update(password.getBytes());
        byte[] mac = m.doFinal();
        return toHexString(mac);
    } catch (Exception e) {
        System.out.println(e.toString());
    }

    return StringUtils.EMPTY;
}