Skip to content

Commit 122286c

Browse files
committed
MD5
1 parent 116e9c4 commit 122286c

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## v1.2.0 - 2024-02-15
4+
5+
- Added the MD5 hash algorithm. This algorithm is broken and not recommended
6+
for security purposes, but may be useful for interoperability with systems
7+
that use MD5.
8+
39
## v1.1.0 - 2024-02-15
410

511
- This library now supports the JavaScript target too.

src/gleam/crypto.gleam

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ pub type HashAlgorithm {
2424
Sha256
2525
Sha384
2626
Sha512
27+
/// The MD5 hash algorithm is considered weak and should not be used for
28+
/// security purposes. It may still be useful for non-security purposes or for
29+
/// compatibility with existing systems.
30+
Md5
2731
}
2832

2933
/// Computes a digest of the input bit string.
@@ -89,6 +93,7 @@ fn signing_input(digest_type: HashAlgorithm, message: BitArray) -> String {
8993
Sha256 -> "HS256"
9094
Sha384 -> "HS384"
9195
Sha512 -> "HS512"
96+
Md5 -> "Md5"
9297
}
9398
string.concat([
9499
bit_array.base64_url_encode(<<protected:utf8>>, False),

src/gleam_crypto_ffi.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { BitArray } from "./gleam.mjs";
2-
import { Sha224, Sha256, Sha384, Sha512 } from "./gleam/crypto.mjs";
2+
import { Sha224, Sha256, Sha384, Sha512, Md5 } from "./gleam/crypto.mjs";
33
import * as crypto from "node:crypto";
44

55
function webCrypto() {
@@ -18,6 +18,8 @@ function algorithmName(algorithm) {
1818
return "sha384";
1919
} else if (algorithm instanceof Sha512) {
2020
return "sha512";
21+
} else if (algorithm instanceof Md5) {
22+
return "md5";
2123
} else {
2224
throw new Error("Unsupported algorithm");
2325
}

test/gleam/crypto_test.gleam

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ pub fn hash_sha512_test() {
4545
>>)
4646
}
4747

48+
pub fn hash_md5_test() {
49+
crypto.hash(crypto.Md5, <<"hi":utf8>>)
50+
|> should.equal(<<
51+
73, 246, 138, 92, 132, 147, 236, 44, 11, 244, 137, 130, 28, 33, 252, 59,
52+
>>)
53+
}
54+
4855
pub fn hmac_sha256_test() {
4956
<<"Aladin":utf8>>
5057
|> crypto.hmac(crypto.Sha256, <<"secret":utf8>>)
@@ -84,6 +91,14 @@ pub fn hmac_sha512_test() {
8491
>>)
8592
}
8693

94+
pub fn hmac_md5_test() {
95+
<<"Aladin":utf8>>
96+
|> crypto.hmac(crypto.Md5, <<"secret":utf8>>)
97+
|> should.equal(<<
98+
252, 36, 147, 158, 191, 93, 158, 10, 120, 217, 237, 157, 107, 233, 188, 139,
99+
>>)
100+
}
101+
87102
pub fn secure_compare1_test() {
88103
crypto.secure_compare(
89104
bit_array.from_string("ab"),

0 commit comments

Comments
 (0)