modules/ballerina-native/src/main/java/org/ballerinalang/nativeimpl/utils/GetHash.java
@Override public BValue[] execute(Context context) {
        String baseString = getArgument(context, 0).stringValue();
        String algorithm = getArgument(context, 1).stringValue();

        //todo document the supported algorithm
        switch (algorithm) {
            case "SHA1": algorithm = "SHA-1";
                break;
            case "SHA256": algorithm = "SHA-256";
                break;
            case "MD5":
                break;
            default:
                throw new BallerinaException(
                        "Unsupported algorithm " + algorithm + " for HMAC calculation");
        }

        String result;
        try {
            MessageDigest messageDigest;
            messageDigest = MessageDigest.getInstance(algorithm);
            messageDigest.update(baseString.getBytes("UTF-8"));
            byte[] bytes = messageDigest.digest();

            final char[] hexArray = "0123456789ABCDEF".toCharArray();
            char[] hexChars = new char[bytes.length * 2];

            for (int j = 0; j < bytes.length; j++) {
                final int byteVal = bytes[j] & 0xFF;
                hexChars[j * 2] = hexArray[byteVal >>> 4];
                hexChars[j * 2 + 1] = hexArray[byteVal & 0x0F];
            }

            result = new String(hexChars);

        } catch (NoSuchAlgorithmException e) {
            throw new BallerinaException(
                    "Error while calculating HMAC for " + algorithm + ": " + e.getMessage(),
                    context);
        } catch (UnsupportedEncodingException e) {
            throw new BallerinaException("Error while encoding" + e.getMessage(), context);
        }

        return getBValues(new BString(result));
    }
/**
     * Hashes the string contents (assumed to be UTF-8) using the SHA-256 algorithm.
     */
Hashes the string contents (assumed to be UTF-8) using the SHA-256 algorithm.
Hashes the string contents ( assumed to be UTF - 8 ) using the SHA - 256 algorithm .
@Override public BValue[] execute(Context context) {
        String baseString = getArgument(context, 0).stringValue();
        String algorithm = getArgument(context, 1).stringValue();
        switch (algorithm) {
            case "SHA1": algorithm = "SHA-1";
                break;
            case "SHA256": algorithm = "SHA-256";
                break;
            case "MD5":
                break;
            default:
                throw new BallerinaException(
                        "Unsupported algorithm " + algorithm + " for HMAC calculation");
        }
        String result;
        try {
            MessageDigest messageDigest;
            messageDigest = MessageDigest.getInstance(algorithm);
            messageDigest.update(baseString.getBytes("UTF-8"));
            byte[] bytes = messageDigest.digest();
            final char[] hexArray = "0123456789ABCDEF".toCharArray();
            char[] hexChars = new char[bytes.length * 2];
            for (int j = 0; j < bytes.length; j++) {
                final int byteVal = bytes[j] & 0xFF;
                hexChars[j * 2] = hexArray[byteVal >>> 4];
                hexChars[j * 2 + 1] = hexArray[byteVal & 0x0F];
            }
            result = new String(hexChars);
        } catch (NoSuchAlgorithmException e) {
            throw new BallerinaException(
                    "Error while calculating HMAC for " + algorithm + ": " + e.getMessage(),
                    context);
        } catch (UnsupportedEncodingException e) {
            throw new BallerinaException("Error while encoding" + e.getMessage(), context);
        }
        return getBValues(new BString(result));
    }
@ Override public BValue [ ] execute ( Context context ) { String baseString = getArgument ( context , 0 ) . stringValue ( ) ; String algorithm = getArgument ( context , 1 ) . stringValue ( ) ; switch ( algorithm ) { case "SHA1" : algorithm = "SHA-1" ; break ; case "SHA256" : algorithm = "SHA-256" ; break ; case "MD5" : break ; default : throw new BallerinaException ( "Unsupported algorithm " + algorithm + " for HMAC calculation" ) ; } String result ; try { MessageDigest messageDigest ; messageDigest = MessageDigest . getInstance ( algorithm ) ; messageDigest . update ( baseString . getBytes ( "UTF-8" ) ) ; byte [ ] bytes = messageDigest . digest ( ) ; final char [ ] hexArray = "0123456789ABCDEF" . toCharArray ( ) ; char [ ] hexChars = new char [ bytes . length * 2 ] ; for ( int j = 0 ; j < bytes . length ; j ++ ) { final int byteVal = bytes [ j ] & 0xFF ; hexChars [ j * 2 ] = hexArray [ byteVal >>> 4 ] ; hexChars [ j * 2 + 1 ] = hexArray [ byteVal & 0x0F ] ; } result = new String ( hexChars ) ; } catch ( NoSuchAlgorithmException e ) { throw new BallerinaException ( "Error while calculating HMAC for " + algorithm + ": " + e . getMessage ( ) , context ) ; } catch ( UnsupportedEncodingException e ) { throw new BallerinaException ( "Error while encoding" + e . getMessage ( ) , context ) ; } return getBValues ( new BString ( result ) ) ; }
Hashes the string contents (assumed to be UTF-8) using the SHA-256 algorithm.
Hashes the string contents ( assumed to be UTF - 8 ) using the SHA - 256 algorithm .
//todo document the supported algorithm