Skip to content

Commit

Permalink
3.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
andmev committed Oct 1, 2024
1 parent 8873b03 commit 375c083
Show file tree
Hide file tree
Showing 22 changed files with 136 additions and 55 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ license = "MIT"
name = "stegjs"
readme = "README.md"
repository = "https://github.com/andmev/stegjs"
version = "3.0.0"
version = "3.0.1"

[lib]
crate-type = ["cdylib"]
Expand All @@ -24,7 +24,8 @@ napi = { version = "2.16.11", default-features = false, features = [
napi-derive = "2.16.12"
openssl = { version = "0.10", features = ["vendored"] }
regex = "1.10.6"
reqwest = { version = "0.12.7", features = ["json", "stream"] }
reqwest = { version = "0.12.8", features = ["json", "stream"] }
serde = { version = "1.0.210", features = ["derive"] }
tokio = { version = "1.40.0", features = ["full"] }
url = "2.5.2"

Expand Down
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![npm](https://img.shields.io/npm/v/stegjs.svg?maxAge=1)](https://www.npmjs.com/package/stegjs) [![npm](https://img.shields.io/npm/dt/stegjs.svg?maxAge=1)](https://www.npmjs.com/package/stegjs) ![Steg.js Test workflow](https://github.com/andmev/stegjs/actions/workflows/test.yml/badge.svg)
[![npm](https://img.shields.io/npm/l/stegjs.svg?maxAge=1)](https://www.npmjs.com/package/stegjs)

> Command-line utility for steganography in PNG images. With this application you can send secret messages, passwords or other important information.
> A Node.js module and command-line utility for performing steganographic encoding in PNG images. This application enables the secure transmission of secret messages, passwords, or other critical information by embedding data within images.
## Installation

Expand All @@ -13,8 +13,30 @@ To install globally you should enter in terminal window the following command:
$ npm i -g stegjs
```

To install locally you should enter in terminal window the following command:

```sh
$ npm i stegjs
```

## Help

### Usage as Node.js module

```js
const steg = require('stegjs')

// Encode message
const response = steg.encode('img.png', 'my_secret_pass', '1x1', './secrets/go.png')
console.log(response) // -> { message: 'my_secret_pass', pattern: '1x1', output: './secrets/go.png' }

// Decode message
const response = steg.decode('out.png')
console.log(response) // -> { message: 'my_secret_pass', pattern: '1x1' }
```

### Usage as CLI

```sh
$ stegjs --help

Expand Down
4 changes: 3 additions & 1 deletion __test__/decode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import path from 'path'
import { decode } from '../index.js'

test('decode', async (t) => {
await decode(path.join(process.cwd(), '__test__', 'decode.png'))
const result = await decode(path.join(process.cwd(), '__test__', 'decode.png'))
t.pass()
t.is(result.message, 'some-key')
t.is(result.pattern, '1x1')
})

test('decode fails if image not exist', async (t) => {
Expand Down
5 changes: 4 additions & 1 deletion __test__/encode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ test.afterEach(() => {
})

test('encode', async (t) => {
await encode(input, 'some-key', '1x1', output)
const result = await encode(input, 'some-key', '1x1', output)
t.timeout(10000, 'make sure the test is not timeout')
t.is(fs.existsSync(output), true)
t.is(result.pattern, '1x1')
t.is(result.message, 'some-key')
t.is(result.output, output)
})

test('encode fails if image not exist', async (t) => {
Expand Down
13 changes: 11 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@

/* auto-generated by NAPI-RS */

export interface EncodeResult {
output: string
message: string
pattern: string
}
export interface DecodeResult {
message: string
pattern: string
}
/** Exposes the `encode_rs` function to Node.js via N-API. */
export declare function encode(image: string, message: string, step: string, output: string): Promise<void>
export declare function encode(image: string, message: string, step: string, output: string): Promise<EncodeResult>
/** Exposes the `decode_rs` function to Node.js via N-API. */
export declare function decode(image: string): Promise<void>
export declare function decode(image: string): Promise<DecodeResult>
2 changes: 1 addition & 1 deletion npm/android-arm-eabi/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@andmev/stegjs-android-arm-eabi",
"version": "3.0.0",
"version": "3.0.1",
"os": [
"android"
],
Expand Down
2 changes: 1 addition & 1 deletion npm/android-arm64/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@andmev/stegjs-android-arm64",
"version": "3.0.0",
"version": "3.0.1",
"os": [
"android"
],
Expand Down
2 changes: 1 addition & 1 deletion npm/darwin-arm64/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@andmev/stegjs-darwin-arm64",
"version": "3.0.0",
"version": "3.0.1",
"os": [
"darwin"
],
Expand Down
2 changes: 1 addition & 1 deletion npm/darwin-x64/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@andmev/stegjs-darwin-x64",
"version": "3.0.0",
"version": "3.0.1",
"os": [
"darwin"
],
Expand Down
2 changes: 1 addition & 1 deletion npm/freebsd-x64/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@andmev/stegjs-freebsd-x64",
"version": "3.0.0",
"version": "3.0.1",
"os": [
"freebsd"
],
Expand Down
2 changes: 1 addition & 1 deletion npm/linux-arm-gnueabihf/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@andmev/stegjs-linux-arm-gnueabihf",
"version": "3.0.0",
"version": "3.0.1",
"os": [
"linux"
],
Expand Down
2 changes: 1 addition & 1 deletion npm/linux-arm64-gnu/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@andmev/stegjs-linux-arm64-gnu",
"version": "3.0.0",
"version": "3.0.1",
"os": [
"linux"
],
Expand Down
2 changes: 1 addition & 1 deletion npm/linux-arm64-musl/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@andmev/stegjs-linux-arm64-musl",
"version": "3.0.0",
"version": "3.0.1",
"os": [
"linux"
],
Expand Down
2 changes: 1 addition & 1 deletion npm/linux-x64-gnu/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@andmev/stegjs-linux-x64-gnu",
"version": "3.0.0",
"version": "3.0.1",
"os": [
"linux"
],
Expand Down
2 changes: 1 addition & 1 deletion npm/linux-x64-musl/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@andmev/stegjs-linux-x64-musl",
"version": "3.0.0",
"version": "3.0.1",
"os": [
"linux"
],
Expand Down
2 changes: 1 addition & 1 deletion npm/win32-arm64-msvc/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@andmev/stegjs-win32-arm64-msvc",
"version": "3.0.0",
"version": "3.0.1",
"os": [
"win32"
],
Expand Down
2 changes: 1 addition & 1 deletion npm/win32-ia32-msvc/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@andmev/stegjs-win32-ia32-msvc",
"version": "3.0.0",
"version": "3.0.1",
"os": [
"win32"
],
Expand Down
2 changes: 1 addition & 1 deletion npm/win32-x64-msvc/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@andmev/stegjs-win32-x64-msvc",
"version": "3.0.0",
"version": "3.0.1",
"os": [
"win32"
],
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "stegjs",
"version": "3.0.0",
"version": "3.0.1",
"description": "Encrypt message to PNG image.",
"main": "index.js",
"types": "index.d.ts",
Expand Down
54 changes: 31 additions & 23 deletions src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::error::Error;
use crate::checker::{is_png, is_uri};
use crate::converters::{bits_to_string, meta_to_obj};
use crate::get_file::{by_path, by_uri};
use crate::DecodeResult; // Add this import

/// Decodes a hidden message from a PNG image using steganography.
///
Expand All @@ -12,7 +13,7 @@ use crate::get_file::{by_path, by_uri};
/// # Errors
/// Returns an error if the input image is not a valid PNG, if decoding fails,
/// or if there are issues reading the files.
pub async fn decode_rs(img: &str) -> Result<(), Box<dyn Error>> {
pub async fn decode_rs(img: &str) -> Result<DecodeResult, Box<dyn Error>> {
// Check that the input file is in PNG format.
if !is_png(img) {
return Err("Only *.png images are supported.".into());
Expand All @@ -26,9 +27,9 @@ pub async fn decode_rs(img: &str) -> Result<(), Box<dyn Error>> {
};

// Proceed to decode the message from the image.
decode_image(&file_path)?;
let (message, pattern) = decode_image(&file_path)?;

Ok(())
Ok(DecodeResult { message, pattern })
}

/// Helper function to decode the message from the image.
Expand All @@ -38,14 +39,14 @@ pub async fn decode_rs(img: &str) -> Result<(), Box<dyn Error>> {
///
/// # Errors
/// Returns an error if decoding fails or if the message cannot be reconstructed.
fn decode_image(file_path: &str) -> Result<(), Box<dyn Error>> {
fn decode_image(file_path: &str) -> Result<(String, String), Box<dyn Error>> {
// Load the image from the specified path.
let img = image::open(file_path)?.to_rgba8();
let (width, height) = img.dimensions();

// Define the number of bits to read for meta-information.
// Assuming meta string is 8 characters: "10|10"
let meta_str_length = 8;
// Assuming meta string is 8 characters: "10|XX"
let meta_str_length = 8; // Adjusted to match the meta format in encode.rs
let meta_bits_len = meta_str_length * 8;

// Extract meta bits
Expand Down Expand Up @@ -117,20 +118,23 @@ fn decode_image(file_path: &str) -> Result<(), Box<dyn Error>> {
// Convert message bits to string
let message = bits_to_string(&message_bits)?;

// Output the decoded message
// Construct the pattern string
let pattern = format!("{}x{}", width_step_num, height_step_num);

// Output the decoded message and pattern
println!(
"{} was decoded!\nmessage: {}\npattern: {}x{}",
file_path, message, width_step_num, height_step_num
"{} was decoded!\nmessage: {}\npattern: {}",
file_path, message, pattern
);

Ok(())
Ok((message, pattern))
}

#[cfg(test)]
mod tests {
use super::*;
use crate::encode_rs;
use image::{GenericImageView, Rgba, RgbaImage};
use image::{Rgba, RgbaImage};
use tempfile::NamedTempFile;

#[tokio::test]
Expand All @@ -147,7 +151,7 @@ mod tests {
let test_message = "Test message";

// Encode the message
encode_rs(
let encode_result = encode_rs(
temp_in.path().with_extension("png").to_str().unwrap(),
test_message,
"1x1",
Expand All @@ -156,19 +160,23 @@ mod tests {
.await
.expect("Failed to encode message");

// Print the contents of the encoded image
let encoded_img = image::open(temp_out.path().with_extension("png")).unwrap();
println!("Encoded image dimensions: {:?}", encoded_img.dimensions());
// Verify the encode result
assert_eq!(
encode_result.output,
temp_out.path().with_extension("png").to_str().unwrap()
);
assert_eq!(encode_result.message, test_message);
assert_eq!(encode_result.pattern, "1x1");

// Decode the message
let result = decode_rs(temp_out.path().with_extension("png").to_str().unwrap()).await;
let decode_result = decode_rs(temp_out.path().with_extension("png").to_str().unwrap())
.await
.expect("Failed to decode message");

match result {
Ok(_) => println!("Decoding successful"),
Err(e) => {
println!("Decoding failed: {:?}", e);
panic!("Decoding failed: {:?}", e);
}
}
// Verify the decode result
assert_eq!(decode_result.message, test_message);
assert_eq!(decode_result.pattern, "1x1");

println!("Decoding test successful.");
}
}
Loading

0 comments on commit 375c083

Please sign in to comment.