Can sha3 output be used as a filename?
The question in other terms: What are the characters in a sha3 output?
In case this is implementation-specific, I'm using the sha3 npm package.
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');
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.