Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add keccak_256 hashing #578

Merged
merged 26 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from 11 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/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 =
thil4n marked this conversation as resolved.
Show resolved Hide resolved
"73b6cc25ab0656625ee654a6cdc8f1d1803a6330fba4f4bf5bd6b9018f7d3131".toLowerAscii();
test:assertEquals(hashKeccak256(input).toBase16(), expectedKeccak256Hash);
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.params.RSAKeyParameters;
import org.bouncycastle.jcajce.SecretKeyWithEncapsulation;
import org.bouncycastle.jcajce.provider.digest.Keccak;
import org.bouncycastle.jcajce.spec.KEMExtractSpec;
import org.bouncycastle.jcajce.spec.KEMGenerateSpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
Expand Down Expand Up @@ -124,7 +125,14 @@ public static Object hmac(String algorithm, byte[] key, byte[] input) {
*/
public static byte[] hash(String algorithm, byte[] input, Object salt) {
try {
MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
MessageDigest messageDigest;

if ("Keccak-256".equalsIgnoreCase(algorithm)) {
thil4n marked this conversation as resolved.
Show resolved Hide resolved
messageDigest = new Keccak.Digest256();
} else {
messageDigest = MessageDigest.getInstance(algorithm);
}

if (salt != null) {
messageDigest.update(((BArray) salt).getBytes());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,8 @@ public static BArray hashSha512(BArray inputValue, Object saltValue) {
return ValueCreator.createArrayValue(CryptoUtils.hash("SHA-512", inputValue.getBytes(), saltValue));
}

public static BArray hashKeccak256(BArray inputValue, Object saltValue) {
return ValueCreator.createArrayValue(CryptoUtils.hash("Keccak-256", inputValue.getBytes(), saltValue));
thil4n marked this conversation as resolved.
Show resolved Hide resolved
}

thil4n marked this conversation as resolved.
Show resolved Hide resolved
}