0
  1. Can sha3 output be used as a filename?

  2. The question in other terms: What are the characters in a sha3 output?

  3. In case this is implementation-specific, I'm using the sha3 npm package.

Hossam El-Deen
  • 972
  • 1
  • 13
  • 27
  • 1
    A better question is: why? – President James K. Polk Dec 05 '18 at 03:27
  • In Beaker Browser, a dat repository, which is something like a git repository but can shared easily like bittorrent, a dat repository can be used as a key-value database, where they filename is the key. In a use-case I have, I want a dat repository to contain the list of some strings which I don't want to expose, but I want anyone to be query if a given string is in this list or no. Makes sense? :) – Hossam El-Deen Dec 05 '18 at 08:57

1 Answers1

1

Short answer

filename in the code below can be used safely as a filename, but take note that there's usually a maximum length on a file name and path in many operating systems and filename will be 128 characters long.

By a quick google, seems like the maximum file length on Linux is usually 255 characters bytes and the maximum path length is 4096 characters. On Windows, there might be still the limit of a maximum path length of 260 characters, so take note.

import { SHA3 } from 'sha3';

const hash = new SHA3(512);

hash.update('foo');
const filename = hash.digest('hex');

Longer answer

The output of SHA-3 is 512 bits. Using the package linked in the question, hash.diget() (without arguments) returns a Buffer containing 64 elements * 8 bits per element = 512 bits. If you call hash.digest('hex') it'll return a hexadecimal string consisting of only of characters 0-9 and a-f which are all safe in a filename. See digest's documentation for other output formats.

Note that sha-3 can be used with some output lengths other than 512 (224, 256, 384); the explanation would be the same but for the different number.

Surely, one can do better (make a shorter file name), but this is good enough for my purposes. Also, there's nothing sha3-specific here actually; any binary data can be encoded as a hexadecimal string that can be used as a filename; just take note of the length.

Hossam El-Deen
  • 972
  • 1
  • 13
  • 27
  • @kelalaka thank you. Can I understand from this that actually Linux filename limit is not 255 characters, but will work in this case just because hexadecimal utf8 strings happen to be stored in one byte? – Hossam El-Deen Dec 05 '18 at 08:58
  • 1
    It might be worth explicitly noting that there's nothing specific to SHA-3 here; _any_ binary data can be used as a filename if it has been hex-encoded (and is not too long). Also, there are more compact encodings than hex that could be used for the same purpose, such as [URL-safe base64](https://en.wikipedia.org/wiki/Base64#URL_applications). – Ilmari Karonen Dec 05 '18 at 14:52
  • Yeah, I agree. Feel free to edit the answer if you want. Btw, do you have to know an npm implementation for URL-safe base64? @IlmariKaronen – Hossam El-Deen Dec 05 '18 at 23:20