[Proposal] File.checksum/2

93 views
Skip to first unread message

matthi...@railsmechanic.de

unread,
Jun 13, 2017, 9:43:40 AM6/13/17
to elixir-lang-core
Hello everybody, 

I propose adding a File.checksum/2 function to create a (md5/sha1/sha256/...) checksum of a given file path.
The function might have the following signature:

checksum(String.t, Atom.t) :: {:ok, String.t} | {:error, String.t}

- The first attribute is the path to the file where a checksum should be computed.
- The second attribute addresses the used hashing algorithm as an atom e.g. :md5, :sha, :sha256, according to the the algorithms provided by erlang.

After searching the web for quite a long time, it was hard to find a suitable AND efficient implementation of creating file checksums with Elixir.
Therefore, I’m convinced this function will help many developers creating file checksums.

Regards,
Matthias

Amos King

unread,
Jun 13, 2017, 10:23:04 AM6/13/17
to elixir-l...@googlegroups.com
I think that those algorithms are useful for more than files and already exist in Erlang. What advantage do you see over using

File.open(path) |> :crypto.hash(:md5)

That isn't streaming but I think it is very clear. 

Amos King
Binary Noggin
--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/ee960cf6-b4dd-4638-acc4-a3eee37c63aa%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michał Muskała

unread,
Jun 13, 2017, 10:51:27 AM6/13/17
to elixir-l...@googlegroups.com
I think we could go for something more general instead. Right now it's very easy to define stream "sources" with various functions such as Stream.transform or Stream.resource. It's not as easy to create a "sink" stream that would accept the input lazily. This could easily allow solving the issue at hand in a fairly clean manner, for example:

sink = 
  Stream.consume(
    fn -> :crypto.hash_init(:md5) end,
    fn acc, data -> :crypto.hash_update(acc, data) end,
    fn acc -> :crypto.hash_final(acc) end)

Enum.into(File.stream!(path), sink)

I think there will be many instances where the collectable implementation could be expressed in terms of this operation without requiring the full-blown struct and protocol implementation.

Michał.
Reply all
Reply to author
Forward
0 new messages