DEV Community

Cover image for horcrux: Split files into encrypted fragments for Node & React Native (TypeScript library)
nchika
nchika

Posted on

horcrux: Split files into encrypted fragments for Node & React Native (TypeScript library)

Horcrux is a TypeScript library for splitting files into encrypted fragments — “horcruxes” — inspired by Harry Potter. Think of it as digital horcruxes for your sensitive files. You can split your files into multiple pieces where only a subset is needed to restore the original — no password required.

The original idea comes from jesseduffield/horcrux — the CLI tool from the lazygit author that slices a file into N shares and restores it when any M shares are present.

Horcrux

I wanted a version that plugs into both Node and React Native projects, so “Yet Another Horcrux” became a library.

Repo: https://github.com/nao1215/horcrux

npm: https://www.npmjs.com/package/@nao1215/horcrux


How to use

The API stays simple: use split and bind for files, splitBuffer and bindHorcruxes if you’re juggling data in memory. Both sides run on Node and React Native.

Use files

import { split, bind, nodeAdapter } from '@nao1215/horcrux';
import { saveHorcruxes } from '@nao1215/horcrux/core/split';

// Split a file into 5 pieces, need 3 to restore
const result = await split('secret.pdf', 5, 3);

// Save horcruxes (creates secret_1_of_5.horcrux, etc.)
const files = await saveHorcruxes(result.horcruxes, './output', nodeAdapter);

// Later: Restore from any 3 horcruxes
await bind(files.slice(0, 3), 'restored_secret.pdf');
Enter fullscreen mode Exit fullscreen mode

Use buffer

import { splitBuffer } from '@nao1215/horcrux';
import { bindHorcruxes } from '@nao1215/horcrux/core/bind';

const data = Buffer.from('Secret message');
const result = await splitBuffer(data, 'message.txt', {
  total: 4,
  threshold: 2
});

const restored = await bindHorcruxes(result.horcruxes.slice(0, 2));
console.log(restored.data.toString()); // 'Secret message'
Enter fullscreen mode Exit fullscreen mode

How it works

Under the hood it’s Shamir’s Secret Sharing (SSS).

First the file is encrypted, then the encryption key is encoded as coefficients of a polynomial. Each share gets the ciphertext plus a polynomial evaluation. Set a threshold t and total n; as soon as t shares show up, Lagrange interpolation reconstructs the polynomial and the key drops out.

Sit at t-1 and you’re stuck with infinite candidates — guessing isn’t happening. Each share isn’t just “n equal slices”; it carries redundant structure to make the threshold trick work while staying useless alone.


Why did I make this?

In Japan, there’s an internet meme: “If I die, destroy my HDD (or SSD).”
My counterpoint: “I’ve split my files with horcrux, so unless you know the restoration procedure, you can’t see what’s inside — no need to destroy anything, right?”


Status & feedback

I’ve tested on both Node and React Native and it works, but the React Native side is less battle-tested — bug reports and feedback are welcome.

If this looks useful, a ⭐️ on GitHub would mean a lot!

Top comments (0)