gleam-lang / crypto Goto Github PK
View Code? Open in Web Editor NEW⛓️ Crypto functionality for Gleam applications
Home Page: https://hexdocs.pm/gleam_crypto/
License: Apache License 2.0
⛓️ Crypto functionality for Gleam applications
Home Page: https://hexdocs.pm/gleam_crypto/
License: Apache License 2.0
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.
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
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.
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 @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:
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.