How do I compute the SHA1 sum of a File?

I am working on a simple S3 image uploader in Elm. I’d like to name the uploaded file by a prefix of its SHA1 sum, as a primitive deduplication effort and also to supply a reasonable unique name for the file.

I use an ordinary input[type="file"] to get the file, which means I receive a File type back. How do I compute a SHA1 of that? I tried TSFoster/elm-sha1, but that needs the data as List Int, whereas File.toBytes gets me Bytes. Conversion sounds expensive. How do I proceed? (Preferably without bailing out to JavaScript.)

Conversion shouldn’t be expensive. I haven’t tried this, but looking into the codebase of the packages you’re using, the SHA1.fromBytes method that needs List Int is initially filtering such a list between 0–255.

That means that you can parse your Bytes data with Bytes.Decode.unsignedInt8 before sending it to the sha1 routine. Internally, this is a javascript call, but I guess you’re fine with an elm kernel implementation over a set of ports.

1 Like

Sounds like a neat project. I’ve replied on the Github issue you created, hopefully you’ll be able to work it out, or wait for a few weeks for me to take a look :-).

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.