org.apache.commons.codec.digest.HmacAlgorithms Java Examples

The following examples show how to use org.apache.commons.codec.digest.HmacAlgorithms. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: matrix-java-sdk   Author: kamax-matrix   File: MatrixHttpClient.java    License: GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public void register(MatrixPasswordCredentials credentials, String sharedSecret, boolean admin) {
    // As per synapse registration script:
    // https://github.com/matrix-org/synapse/blob/master/scripts/register_new_matrix_user#L28

    String value = credentials.getLocalPart() + "\0" + credentials.getPassword() + "\0"
            + (admin ? "admin" : "notadmin");
    String mac = new HmacUtils(HmacAlgorithms.HMAC_SHA_1, sharedSecret).hmacHex(value);
    JsonObject body = new JsonObject();
    body.addProperty("user", credentials.getLocalPart());
    body.addProperty("password", credentials.getPassword());
    body.addProperty("mac", mac);
    body.addProperty("type", "org.matrix.login.shared_secret");
    body.addProperty("admin", false);
    URL url = getPath("client", "api", "v1", "register");
    updateContext(execute(new Request.Builder().post(getJsonBody(body)).url(url)));
}
 
Example #2
Source Project: JuniperBot   Author: JuniperBot   File: PatreonServiceImpl.java    License: GNU General Public License v3.0 6 votes vote down vote up
@PostConstruct
private void init() {
    List<PatreonUser> activeUsers = repository.findActive();
    activeUsers.forEach(this::enableFeatures);

    String accessToken = workerProperties.getPatreon().getAccessToken();
    if (StringUtils.isNotEmpty(accessToken)) {
        creatorApi = new PatreonAPI(accessToken);
        if (workerProperties.getPatreon().isUpdateEnabled()) {
            scheduler.scheduleWithFixedDelay(this::update, workerProperties.getPatreon().getUpdateInterval());
        }
    } else {
        log.warn("No Patreon credentials specified, integration would not work");
    }
    String webhookSecret = workerProperties.getPatreon().getWebhookSecret();
    if (StringUtils.isNotEmpty(webhookSecret)) {
        webHookHmac = new HmacUtils(HmacAlgorithms.HMAC_MD5, webhookSecret);
    } else {
        log.warn("No Patreon WebHook secret specified, WebHooks would not work");
    }
}
 
Example #3
Source Project: instamojo-java   Author: Instamojo   File: InstamojoImpl.java    License: MIT License 6 votes vote down vote up
@Override
public String generateWebhookSignature(Map<String, String> data, String salt) {

    ArrayList<String> keys = new ArrayList<>(data.keySet());
    Collections.sort(keys);

    StringBuilder sb = new StringBuilder();
    for (int index = 0; index < keys.size(); index++) {
        sb.append(data.get(keys.get(index)));
        if (index != keys.size() - 1) {
            sb.append('|');
        }
    }

    return new HmacUtils(HmacAlgorithms.HMAC_SHA_1, salt).hmacHex(sb.toString());
}
 
Example #4
Source Project: gocd   Author: gocd   File: BitBucketServerAuth.java    License: Apache License 2.0 6 votes vote down vote up
default void validateAuth(String webhookSecret) {
    if (exemptFromAuth()) {
        return;
    }

    String signature = request().headers("X-Hub-Signature");

    if (isBlank(signature)) {
        throw die("No HMAC signature specified via 'X-Hub-Signature' header!");
    }

    String expectedSignature = "sha256=" + new HmacUtils(HmacAlgorithms.HMAC_SHA_256, webhookSecret)
            .hmacHex(request().body());

    if (!MessageDigest.isEqual(expectedSignature.getBytes(), signature.getBytes())) {
        throw die("HMAC signature specified via 'X-Hub-Signature' did not match!");
    }

    if (!"git".equals(scmType())) {
        throw die("Only 'git' repositories are currently supported!");
    }
}
 
Example #5
Source Project: alf.io   Author: alfio-event   File: ExtensionUtils.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static String computeHMAC(String secret, String... parts) {
    if(parts == null || parts.length == 0) {
        return "";
    }
    var text = Arrays.stream(parts).map(StringUtils::trimToEmpty).collect(Collectors.joining(""));
    return new HmacUtils(HmacAlgorithms.HMAC_SHA_256, secret).hmacHex(text);
}
 
Example #6
Source Project: gocd   Author: gocd   File: GitHubAuth.java    License: Apache License 2.0 5 votes vote down vote up
default void validateAuth(String secret) {
    String signature = request().headers("X-Hub-Signature");

    if (isBlank(signature)) {
        throw die("No HMAC signature specified via 'X-Hub-Signature' header!");
    }

    String expectedSignature = "sha1=" + new HmacUtils(HmacAlgorithms.HMAC_SHA_1, secret).hmacHex(request().body());

    if (!MessageDigest.isEqual(expectedSignature.getBytes(), signature.getBytes())) {
        throw die("HMAC signature specified via 'X-Hub-Signature' did not match!");
    }
}
 
Example #7
Source Project: littleca   Author: dushitaoyuan   File: SimpleTokenManager.java    License: Apache License 2.0 4 votes vote down vote up
public SimpleTokenManager(String hmacKey) {
	this.hmac=new HmacUtils(HmacAlgorithms.HMAC_MD5, hmacKey);
}
 
Example #8
Source Project: littleca   Author: dushitaoyuan   File: SimpleTokenManager.java    License: Apache License 2.0 4 votes vote down vote up
public  String createToken(String hmacKey,Map<String,String> data) {
	String signData=mapToString(data);
	String sign = Base64.encodeBase64URLSafeString(new HmacUtils(HmacAlgorithms.HMAC_MD5, hmacKey).hmac(signData));
	String token=Base64.encodeBase64URLSafeString(signData.getBytes())+"."+sign;
	return token;
}
 
Example #9
Source Project: littleca   Author: dushitaoyuan   File: SimpleTokenManager.java    License: Apache License 2.0 4 votes vote down vote up
public SimpleTokenManager(String hmacKey) {
	this.hmac=new HmacUtils(HmacAlgorithms.HMAC_MD5, hmacKey);
}
 
Example #10
Source Project: littleca   Author: dushitaoyuan   File: SimpleTokenManager.java    License: Apache License 2.0 4 votes vote down vote up
public  String createToken(String hmacKey,Map<String,String> data) {
	String signData=mapToString(data);
	String sign = Base64.encodeBase64URLSafeString(new HmacUtils(HmacAlgorithms.HMAC_MD5, hmacKey).hmac(signData));
	String token=Base64.encodeBase64URLSafeString(signData.getBytes())+"."+sign;
	return token;
}
 
Example #11
Source Project: prebid-server-java   Author: rubicon-project   File: FacebookBidder.java    License: Apache License 2.0 4 votes vote down vote up
private String makeAuthId(String requestId) {
    final Mac mac = HmacUtils.getInitializedMac(HmacAlgorithms.HMAC_SHA_256, appSecret.getBytes());
    return Hex.encodeHexString(mac.doFinal(requestId != null ? requestId.getBytes() : null));
}
 
Example #12
Source Project: pay-publicapi   Author: alphagov   File: AuthorizationValidationFilter.java    License: MIT License 4 votes vote down vote up
private boolean tokenMatchesHmac(String token, String currentHmac) {
    final String hmacCalculatedFromToken = BaseEncoding.base32Hex()
            .lowerCase().omitPadding()
            .encode(new HmacUtils(HmacAlgorithms.HMAC_SHA_1, apiKeyHmacSecret).hmac(token));
    return hmacCalculatedFromToken.equals(currentHmac);
}
 
Example #13
Source Project: pay-publicapi   Author: alphagov   File: ApiKeyGenerator.java    License: MIT License 4 votes vote down vote up
public static String apiKeyValueOf(String token, String secret) {
    byte[] hmacBytes = new HmacUtils(HmacAlgorithms.HMAC_SHA_1, secret).hmac(token);
    String encodedHmac = BaseEncoding.base32Hex().lowerCase().omitPadding().encode(hmacBytes);
    return token + encodedHmac;
}
 
Example #14
Source Project: alf.io   Author: alfio-event   File: PayPalManager.java    License: GNU General Public License v3.0 4 votes vote down vote up
private static String computeHMAC(CustomerName customerName, String email, String billingAddress, Event event) {
    return new HmacUtils(HmacAlgorithms.HMAC_SHA_256, event.getPrivateKey()).hmacHex(StringUtils.trimToEmpty(customerName.getFullName()) + StringUtils.trimToEmpty(email) + StringUtils.trimToEmpty(billingAddress));
}
 
Example #15
Source Project: alf.io   Author: alfio-event   File: Ticket.java    License: GNU General Public License v3.0 4 votes vote down vote up
public static String hmacSHA256Base64(String key, String code) {
    return Base64.getEncoder().encodeToString(new HmacUtils(HmacAlgorithms.HMAC_SHA_256, key).hmac(code));
}