A Gleam cryptography library supporting Erlang and JavaScript.
gleam add gleam_crypto
⛓️ Crypto functionality for Gleam applications
Home Page: https://hexdocs.pm/gleam_crypto/
License: Apache License 2.0
Should probably be a 0.1.0 because I don't think we've found a stable API yet,
Was previously depending on a git version of stdlib but features now exist in stdlib 0.10.0
NOTE gleam_bitwise depends on an uncompatible version of stdlib currently, can either post a new gleam_bitwise version to hex otherwise will require stdlib to be specified in the deps.
The current implementation is this:
pub fn hash(a: HashAlgorithm, b: BitArray) -> BitArray
I can't imagine ever calling this function this way
get_dynamic_hash_algorithm()
|> crypto.hash(<<1,2,3,4>>)
it would be way more helpful if it had this signature
pub fn hash(a: BitArray, b: HashAlgorithm) -> BitArray
so it could be called like this instead
extract_bencode(info_field)
|> crypto.hash(crypto.Sha1)
I'm a little bit skeptical about how this would be implemented though, given that it's a pretty major breaking change. But hey a major release doesn't mean nothing can ever be broken right?
I was sent here by tynan on Discord
Hi @lpil!
I want to contribute and add encryption support to the standard library. I am still new to gleam and would appreciate some guidance as it seems to be a pretty big undertaking. I will use this issue to track my progress and write down how I build this API.
Before I start, here are a few questions I had:
I suppose that evantually this library will have other algorithms like BCrypt and Argon2 (more suitable for password hashing).
But putting these in the Crypto
module will be confusing. So it would make sense to have modules like Sha
, Argon2
, ... instead.
Consider renaming the current Crypto
module to Sha
or SecureHash
Thanks
Hi there!
A hash_string
function would be very nice to have, instead of being required to use BitString
.
Let's say we want to hash a string composed of other strings without compromising performance (a very common operation with crypto-releated stuff).
With Elixir, we could do something like this:
def hash_block(block) do
:crypto.hash(:sha256, [block.id, block.data, Integer.to_string(block.timestamp])
end
With Gleam this is a little bit more complicated, since crypto.hash method requires a BitString. The code above would probably look like something like this:
pub fn hash_block(block: Block) {
bit_builder.from_string(block.id)
|> bit_builder.append_string(block.data)
|> bit_builder.append_string(int.to_string(block.timestamp))
|> bit_builder.to_bit_string
|> crypto.hash(crypto.Sha256, _)
...
}
This is obviously too much.
Instead, there could be a function that takes String
as an input. The code above would look something like this:
pub fn hash_block(block: Block) {
string_builder.from_strings([
block.id,
block.data,
int.to_string(block.timestamp)
])
|> string_builder.to_string
|> crypto.hash_string(crypto.Sha256, _)
...
}
Much better!
NOTE: I tried adding the function and it works just fine, passes all tests + new ones I added for this purpose specifically. I don't think it will cause any problems since Gleam already uses UTF-8 binaries under the hood. If it sounds good to you guys I can create the pull request with everything.
Javascript and Rust are widely used on the Ethereum ecosystem and I can see a future where they can use Gleam a simpler language than Rust with static typing to replace Javascript.
I suggest adding keccak256
to enable libraries to use the standard gleam/crypto
lib.
Hey, so this is my very first time fuddling around with Gleam; I'm sorry if this is an issue on my side, but I couldn't find anything on the internet about this otherwise.
I'm following this article to run a Gleam webserver. I copied the sample code:
import gleam/erlang/process
import mist
import wisp
pub fn main() {
wisp.configure_logger()
let assert Ok(_) =
wisp.mist_handler(fn(_) { todo }, "secret_key")
|> mist.new
|> mist.port(8000)
|> mist.start_http
process.sleep_forever()
}
but running gleam run
shows the following error:
$ gleam run
Compiling gleam_crypto
error: Inexhaustive patterns
┌─ ~/gleamtest/build/packages/gleam_crypto/src/gleam/crypto.gleam:47:3
│
47 │ ╭ case left, right {
48 │ │ <<x, left:bits>>, <<y, right:bits>> -> {
49 │ │ let accumulator =
50 │ │ int.bitwise_or(accumulator, int.bitwise_exclusive_or(x, y))
· │
53 │ │ <<>>, <<>> -> accumulator == 0
54 │ │ }
│ ╰───^
This case expression does not have a pattern for all possible values.
If it is run on one of the values without a pattern then it will crash.
The missing patterns are:
_
It looks like not all cases are matched in the gleam_crypto
library
gleam version:
gleam 1.2.1
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.