npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

steg

v0.1.5

Published

Encrypt and hide arbitrary data inside png images

Downloads

133

Readme

Steg

Simple and secure steganography

Encrypt and hide arbitrary data inside png images. Experimental, use at your own risk.

  1. Encodes data using least significant bits of PNG pixels.
  2. Only PNGs are supported, JPGs cannot be used - every time you save them, they can get re-encoded and some data would be lost, which is a no-go for steganography
  3. Encrypts hidden data and its metadata with AES-GCM-256. Warning: LSB bitsTaken are not encrypted for now.

Consider an example, a file.txt with 11-byte hello world content. Here's a brief flow of what the library would do:

  1. Calculate the capacity of png image at given bitsTaken and save it into capacity
  2. Create a flat byte array structure with 5 fields ABCDE, that represents file and its metadata:
    • A bytes 0..1 name length, 4GB max
    • B bytes 1..[1+name length] name, 32 bytes max
    • C bytes B..B+4 file size length, 4GB max
    • D bytes C..[C+file length] file contents
    • E bytes D..end padding filled with zeros — zeros are okay, since we encrypt them
  3. Encrypt ABCDE under given AES key with AES-GCM-256:
    • IV bytes 0..12 taken from CSPRNG
    • ciphertext bytes 12..(end-16) encrypted ABCD
    • auth tag bytes end-16..end GCM authentication tag

So, in the end, 11-byte hello world text content would need at least 11 + 41 (1+8+4+12+16) bytes of capacity inside the png under the given bitsTaken. In any case, it would consume the whole capacity e.g. 500KB and fill it with encrypted AES output in order to thwart detection.

Usage

npm install steg

Can be only used inside browsers. node.js usage with node-canvas that polyfills Canvas API is possible, but had not been tested.

Hide a text file

Select a png image from the web page and uses it to hide file.txt containing hello world

import { RawFile, StegImage, utils } from 'steg';

const file = new RawFile(utils.utf8ToBytes('hello world'), 'file.txt');
const encryptionKey = crypto.getRandomValues(new Uint8Array(32));

const el = document.querySelector('.user-avatar');
const png = new StegImage(el);
const hiddenPngUrl = await png.hide(file, encryptionKey);

Use password to protect files

Using @noble/hashes library and Scrypt to derive AES key from an arbitrary password

import { scrypt } from '@noble/hashes/scrypt';
const passwordBasedKey = scrypt('some-secure-password', 'secure-salt', { N: 2**19, r: 8, p: 1 });
const hiddenPngUrl = await png.hide(file, passwordBasedKey);

Adjust bits used

  • bitsTaken (default: 1) can be an integer from 1 to 8. Lower values makes it harder to detect steganography.
const hiddenBiggerPngUrl = await png.hide(file, encryptionKey, 5); // Uses 5 bitsTaken

HTML file upload form

The form has two upload inputs: one for image inside which the data will be hidden, one for the data itself.

It also has the password and the bit range selector.

<p><input type="file" id="image" accept="image/*" /><label for="image">Image</label></p>
<p><input type="file" id="data" disabled /><label for="data">File to hide inside image</label></p>
<p><input type="password" id="password" disabled /><label for="password">Password</label></p>
<p><input type="range" id="bits" min="1" max="8" value="1" /><label for="bits">Bits taken</label></p>
<div class="steg-output-container"><img id="output" /></div>

Form script:

const { RawFile, StegImage, utils } = steg;
const cache = {};
const el = (s) => document.querySelector(s);
async function hideDataIntoImage() {
  if (!cache.password) return;
  const bitsTaken = Number.parseInt(el('#bits').value);
  const url = await cache.stegImg.hide(cache.hiddenFile, cache.password, bitsTaken);
  await utils.setImageSource(el('#output'), url);
}
el('#password').addEventListener('change', (ev) => {
  cache.password = ev.target.value;
});
el('#image').addEventListener('change', async (ev) => {
  const img = await RawFile.fromFileInput(ev.target);
  cache.stegImg = await StegImage.fromBytesOrURL(img.data);
  el('#data').disabled = false;
});
el('#data').addEventListener('change', async (ev) => {
  cache.hiddenFile = await RawFile.fromFileInput(ev.target);
  hideDataIntoImage();
});
el('#steg-bits').addEventListener('change', (ev) => {
  el('#steg-bits-value').textContent = ev.target.value;
  hideDataIntoImage();
});

License

MIT (c) Paul Miller (https://paulmillr.com), see LICENSE file.