Search in sources :

Example 6 with BEnumerator

use of org.ballerinalang.model.values.BEnumerator in project ballerina by ballerina-lang.

the class GetHmac method execute.

@Override
public void execute(Context context) {
    String baseString = context.getStringArgument(0);
    String keyString = context.getStringArgument(1);
    BEnumerator algorithm = (BEnumerator) context.getRefArgument(0);
    String hmacAlgorithm;
    // todo document the supported algorithm
    switch(algorithm.getName()) {
        case "SHA1":
            hmacAlgorithm = "HmacSHA1";
            break;
        case "SHA256":
            hmacAlgorithm = "HmacSHA256";
            break;
        case "MD5":
            hmacAlgorithm = "HmacMD5";
            break;
        default:
            throw new BallerinaException("Unsupported algorithm " + algorithm + " for HMAC calculation");
    }
    String result;
    try {
        byte[] keyBytes = keyString.getBytes(Charset.defaultCharset());
        SecretKey secretKey = new SecretKeySpec(keyBytes, hmacAlgorithm);
        Mac mac = Mac.getInstance(hmacAlgorithm);
        mac.init(secretKey);
        byte[] baseStringBytes = baseString.getBytes(Charset.defaultCharset());
        result = HashUtils.toHexString(mac.doFinal(baseStringBytes));
    } catch (IllegalArgumentException | InvalidKeyException | NoSuchAlgorithmException e) {
        throw new BallerinaException("Error while calculating HMAC for " + hmacAlgorithm + ": " + e.getMessage(), context);
    }
    context.setReturnValues(new BString(result));
}
Also used : BEnumerator(org.ballerinalang.model.values.BEnumerator) SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) BString(org.ballerinalang.model.values.BString) BString(org.ballerinalang.model.values.BString) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) Mac(javax.crypto.Mac)

Example 7 with BEnumerator

use of org.ballerinalang.model.values.BEnumerator in project ballerina by ballerina-lang.

the class ParseTo method execute.

@Override
public void execute(Context context) {
    String dateString = context.getStringArgument(0);
    BEnumerator pattern = (BEnumerator) context.getRefArgument(0);
    TemporalAccessor parsedDateTime;
    switch(pattern.getName()) {
        case "RFC_1123":
            parsedDateTime = DateTimeFormatter.RFC_1123_DATE_TIME.parse(dateString);
            break;
        default:
            throw new BallerinaException("failed to parse date/time string: " + dateString);
    }
    StructInfo timeZoneStructInfo = Utils.getTimeZoneStructInfo(context);
    StructInfo timeStructInfo = Utils.getTimeStructInfo(context);
    long epochTime = -1;
    String zoneId;
    try {
        epochTime = Instant.from(parsedDateTime).toEpochMilli();
        zoneId = String.valueOf(ZoneId.from(parsedDateTime));
    } catch (DateTimeException e) {
        if (epochTime < 0) {
            throw new BallerinaException("failed to parse \"" + dateString + "\" to the " + pattern.getName() + " format");
        }
        zoneId = ZoneId.systemDefault().toString();
    }
    BStruct timeStruct = Utils.createTimeStruct(timeZoneStructInfo, timeStructInfo, epochTime, zoneId);
    context.setReturnValues(timeStruct);
}
Also used : BEnumerator(org.ballerinalang.model.values.BEnumerator) BStruct(org.ballerinalang.model.values.BStruct) TemporalAccessor(java.time.temporal.TemporalAccessor) DateTimeException(java.time.DateTimeException) StructInfo(org.ballerinalang.util.codegen.StructInfo) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Aggregations

BEnumerator (org.ballerinalang.model.values.BEnumerator)7 BString (org.ballerinalang.model.values.BString)5 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 BStruct (org.ballerinalang.model.values.BStruct)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 InvalidKeyException (java.security.InvalidKeyException)1 MessageDigest (java.security.MessageDigest)1 DateTimeException (java.time.DateTimeException)1 ZonedDateTime (java.time.ZonedDateTime)1 TemporalAccessor (java.time.temporal.TemporalAccessor)1 Mac (javax.crypto.Mac)1 SecretKey (javax.crypto.SecretKey)1 SecretKeySpec (javax.crypto.spec.SecretKeySpec)1 BEnumType (org.ballerinalang.model.types.BEnumType)1 StructInfo (org.ballerinalang.util.codegen.StructInfo)1 UTF8CPEntry (org.ballerinalang.util.codegen.cpentries.UTF8CPEntry)1