Skip to content
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ The Ballerina `crypto` library facilitates APIs to do operations like hashing, H

### Hashes

The `crypto` library supports generating hashes with 5 different hash algorithms MD5, SHA1, SHA256, SHA384, and SHA512. Also, it supports generating the CRC32B checksum.
The `crypto` library supports generating hashes with 6 different hash algorithms MD5, SHA1, SHA256, SHA384, SHA512, and Keccak256. Also, it supports generating the CRC32B checksum.

### HMAC

Expand Down
2 changes: 1 addition & 1 deletion ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[ballerina]
dependencies-toml-version = "2"
distribution-version = "2201.11.0-20241121-075100-c4c87cbc"
distribution-version = "2201.11.0-20241218-101200-109f6cc7"

[[package]]
org = "ballerina"
Expand Down
2 changes: 1 addition & 1 deletion ballerina/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ The Ballerina `crypto` module facilitates APIs to do operations like hashing, HM

### Hashes

The `crypto` module supports generating hashes with 5 different hash algorithms MD5, SHA1, SHA256, SHA384, and SHA512. Also, it supports generating the CRC32B checksum.
The `crypto` module supports generating hashes with 6 different hash algorithms MD5, SHA1, SHA256, SHA384, SHA512, and Keccak256. Also, it supports generating the CRC32B checksum.

### HMAC

Expand Down
15 changes: 15 additions & 0 deletions ballerina/hash.bal
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,18 @@ public isolated function crc32b(byte[] input) returns string = @java:Method {
name: "crc32b",
'class: "io.ballerina.stdlib.crypto.nativeimpl.Hash"
} external;

# Returns the Keccak-256 hash of the given data.
# ```ballerina
# string dataString = "Hello Ballerina";
# byte[] data = dataString.toBytes();
# byte[] hash = crypto:hashKeccak256(data);
# ```
#
# + input - Value to be hashed
# + salt - Salt to be added
# + return - Hashed output
public isolated function hashKeccak256(byte[] input, byte[]? salt = ()) returns byte[] = @java:Method {
name: "hashKeccak256",
'class: "io.ballerina.stdlib.crypto.nativeimpl.Hash"
} external;
8 changes: 8 additions & 0 deletions ballerina/tests/hash_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,11 @@ isolated function testHashSha512WithSalt() {
"5E78702995D181042420860B111781AFEE88ACD455CAA0367271C78DAE0F69DA").toLowerAscii();
test:assertEquals(hashSha512(input, salt).toBase16(), expectedSha512Hash);
}

@test:Config {}
isolated function testHashKeccak256() {
byte[] input = "Ballerina test".toBytes();
string expectedKeccak256Hash =
"73b6cc25ab0656625ee654a6cdc8f1d1803a6330fba4f4bf5bd6b9018f7d3131".toLowerAscii();
test:assertEquals(hashKeccak256(input).toBase16(), expectedKeccak256Hash);
}
12 changes: 11 additions & 1 deletion docs/spec/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
_Owners_: @shafreenAnfar @bhashinee
_Reviewers_: @shafreenAnfar
_Created_: 2022/08/23
_Updated_: 2024/03/26
_Updated_: 2025/01/20
_Edition_: Swan Lake

## Introduction
Expand All @@ -25,6 +25,7 @@ The conforming implementation of the specification is released and included in t
* 2.4. [SHA384](#24-sha384)
* 2.5. [SHA512](#25-sha512)
* 2.6. [CRC32B](#26-crc32b)
* 2.7. [KECCAK256](#27-keccak256)
3. [HMAC](#3-hmac)
* 3.1. [MD5](#31-md5)
* 3.2. [SHA1](#32-sha1)
Expand Down Expand Up @@ -165,6 +166,15 @@ byte[] data = stringData.toBytes();
string checksum = crypto:crc32b(data);
```

### 2.7. [KECCAK256](#27-keccak256)

This API can be used to create the Hex-encoded KECCAK-256 value of the given data.
```ballerina
string stringData = "Hello Ballerina";
byte[] data = stringData.toBytes();
string checksum = crypto:hashKeccak256(data);
```

## 3. [HMAC](#3-hmac)

The `crypto` library supports generating HMAC with 5 different hash algorithms: MD5, SHA1, SHA256, SHA384, and SHA512.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright (c) 2022, WSO2 LLC. (https://www.wso2.com) All Rights Reserved.
#
# WSO2 LLC. licenses this file to you under the Apache License,
# Version 2.0 (the "License"); you may not use this file except
# in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

Args = --initialize-at-run-time=org.bouncycastle.jcajce.provider.drbg.DRBG\$Default \
--initialize-at-run-time=org.bouncycastle.jcajce.provider.drbg.DRBG\$NonceAndIV \
--features=io.ballerina.stdlib.crypto.svm.BouncyCastleFeature
Loading
Loading