Skip to content

Commit

Permalink
Hashing
Browse files Browse the repository at this point in the history
  • Loading branch information
lpil committed Sep 22, 2024
1 parent a0978f2 commit 7fe3b3e
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 3 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

Examples showing how to do many things in Gleam!

- [Cryptography](#cryptography)
- [Data structures](#data-structures)
- [File system](#file-system)
- [Formats](#formats)

## Cryptography

- [Hashing data](./universal/test/cryptography/hashing_data.gleam)

## Data structures

- [Generating UUIDs](./erlang/test/data_structures/generating_uuids.gleam) (Erlang)
Expand Down
1 change: 1 addition & 0 deletions universal/gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ simplifile = ">= 2.1.0 and < 3.0.0"
xmb = ">= 1.0.0 and < 2.0.0"
gleam_json = ">= 1.0.1 and < 2.0.0"
lustre = ">= 4.4.4 and < 5.0.0"
gleam_crypto = ">= 1.3.0 and < 2.0.0"

[dev-dependencies]
gleeunit = ">= 1.0.0 and < 2.0.0"
2 changes: 2 additions & 0 deletions universal/manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

packages = [
{ name = "filepath", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "EFB6FF65C98B2A16378ABC3EE2B14124168C0CE5201553DE652E2644DCFDB594" },
{ name = "gleam_crypto", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_crypto", source = "hex", outer_checksum = "ADD058DEDE8F0341F1ADE3AAC492A224F15700829D9A3A3F9ADF370F875C51B7" },
{ name = "gleam_erlang", version = "0.26.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "3DF72F95F4716883FA51396FB0C550ED3D55195B541568CAF09745984FD37AD1" },
{ name = "gleam_json", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "thoas"], otp_app = "gleam_json", source = "hex", outer_checksum = "9063D14D25406326C0255BDA0021541E797D8A7A12573D849462CAFED459F6EB" },
{ name = "gleam_otp", version = "0.12.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "CD5FC777E99673BDB390092DF85E34EAA6B8EE1882147496290AB3F45A4960B1" },
Expand All @@ -15,6 +16,7 @@ packages = [
]

[requirements]
gleam_crypto = { version = ">= 1.3.0 and < 2.0.0" }
gleam_json = { version = ">= 1.0.1 and < 2.0.0" }
gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" }
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
Expand Down
36 changes: 36 additions & 0 deletions universal/test/cryptography/hashing_data.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//// # Hashing data
////
//// The gleam_crypto library has a function for hashing data using various
//// popular hash algorithms. It works on all targets.
////
//// ## Dependencies
////
//// - https://hex.pm/packages/gleam_crypto

import gleam/bit_array
import gleam/crypto
import gleeunit/should

pub fn main_test() {
let binary_data = <<1, 2, 3, 4, 5>>

// The `hash` function can be used to hash a bit array of any size and shape.
crypto.hash(crypto.Sha256, binary_data)
|> should.equal(<<
116, 248, 31, 225, 103, 217, 155, 76, 180, 29, 109, 12, 205, 168, 34, 120,
202, 238, 159, 62, 47, 37, 213, 229, 163, 147, 111, 243, 220, 236, 96, 208,
>>)

// Commonly hashes are base64 encoded so they can be printed and shared easily
// as text content.
crypto.hash(crypto.Sha256, binary_data)
|> bit_array.base64_encode(True)
|> should.equal("dPgf4WfZm0y0HW0MzagieMrunz4vJdXlo5Nv89zsYNA=")

// SHA256 is a sensible default, but other hash algorithms are available
crypto.hash(crypto.Sha512, binary_data)
|> bit_array.base64_encode(True)
|> should.equal(
"UFQLxK4xh1/Os4KUNMVePCtm3dcieog6O0zI9s2pZa0XErPuAAj5zuCNqT9SNMGnvw4lcO9W1lKA/+ppG5U+/g==",
)
}
2 changes: 1 addition & 1 deletion universal/test/formats/rendering_html.gleam
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//// # Generate JSON
//// # Rendering HTML
////
//// Lustre is often used for complex stateful HTML applications, but it also
//// makes a great type-safe HTML templating system. It works on all targets.
Expand Down
2 changes: 1 addition & 1 deletion universal/test/formats/rendering_json.gleam
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//// # Generate JSON
//// # Rendering JSON
////
//// The gleam_json package can be used to generate JSON. It works on any
//// target.
Expand Down
2 changes: 1 addition & 1 deletion universal/test/formats/rendering_xml.gleam
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//// # Generate XML
//// # Rendering XML
////
//// The xmb package can be used to generate XML. It works on any target.
////
Expand Down

0 comments on commit 7fe3b3e

Please sign in to comment.