steganography-online-codec 1.0.2

Steganography Online Codec allows you to hide a password encrypted message within the images & photos using AES encryption algorithm with a 256-bit PBKDF2 derived key.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
//! Steganography Online Codec Web API client (Rust).
//!
//! See the product page: <https://www.pelock.com/products/steganography-online-codec>

use std::path::Path;

use base64::Engine;
use reqwest::multipart::{Form, Part};
use serde::Deserialize;

/// Error codes returned by the Steganography Online Codec Web API.
pub mod errors {
    /// Cannot connect to the Web API interface (network error).
    pub const WEBAPI_CONNECTION: i32 = -1;
    /// Success.
    pub const SUCCESS: i32 = 0;
    /// Unknown error.
    pub const UNKNOWN: i32 = 1;
    /// Message is too long for the selected image file.
    pub const MESSAGE_TOO_LONG: i32 = 2;
    /// Image file is too big.
    pub const IMAGE_TOO_BIG: i32 = 3;
    /// Invalid input file or file does not exist.
    pub const INVALID_INPUT: i32 = 4;
    /// Image file format is not supported.
    pub const INVALID_IMAGE_FORMAT: i32 = 5;
    /// Image file is malformed.
    pub const IMAGE_MALFORMED: i32 = 6;
    /// Invalid password.
    pub const INVALID_PASSWORD: i32 = 7;
    /// Provided message is too long for the current license tier.
    pub const LIMIT_MESSAGE: i32 = 9;
    /// Provided password is too long for the current license tier.
    pub const LIMIT_PASSWORD: i32 = 10;
    /// Error while writing output file.
    pub const OUTPUT_FILE: i32 = 99;
    /// License key is invalid or expired.
    pub const INVALID_LICENSE: i32 = 100;
}

/// License information returned by the API (`license` object).
#[derive(Debug, Clone, Deserialize)]
pub struct LicenseInfo {
    #[serde(rename = "activationStatus")]
    pub activation_status: Option<bool>,
    #[serde(rename = "userName")]
    pub user_name: Option<String>,
    #[serde(rename = "type")]
    pub license_type: Option<i64>,
    #[serde(rename = "usagesTotal")]
    pub usages_total: Option<i64>,
    #[serde(rename = "usagesCount")]
    pub usages_count: Option<i64>,
}

/// Current limits returned by the API (`limits` object).
#[derive(Debug, Clone, Deserialize)]
pub struct LimitsInfo {
    #[serde(rename = "maxPasswordLen")]
    pub max_password_len: Option<i64>,
    #[serde(rename = "maxMessageLen")]
    pub max_message_len: Option<i64>,
    #[serde(rename = "maxFileSize")]
    pub max_file_size: Option<i64>,
}

/// Successful API response fields used by the SDK (after `error == SUCCESS`).
#[derive(Debug, Clone)]
pub struct CodecResult {
    pub license: Option<LicenseInfo>,
    pub limits: Option<LimitsInfo>,
    /// Decoded secret message (`decode` only).
    pub message: Option<String>,
}

#[derive(Debug, thiserror::Error)]
pub enum SteganographyError {
    #[error("{error_message}")]
    Api {
        code: i32,
        error_message: String,
        raw: Option<serde_json::Value>,
    },
}

impl SteganographyError {
    pub fn code(&self) -> i32 {
        match self {
            SteganographyError::Api { code, .. } => *code,
        }
    }

    pub fn error_message(&self) -> &str {
        match self {
            SteganographyError::Api { error_message, .. } => error_message,
        }
    }

    pub fn raw(&self) -> Option<&serde_json::Value> {
        match self {
            SteganographyError::Api { raw, .. } => raw.as_ref(),
        }
    }
}

/// Default Steganography Online Codec Web API endpoint.
pub const DEFAULT_API_URL: &str = "https://www.pelock.com/api/steganography-online-codec/v1";

/// Client for the Steganography Online Codec Web API.
pub struct SteganographyOnlineCodec {
    api_key: Option<String>,
    api_url: String,
    client: reqwest::Client,
}

impl SteganographyOnlineCodec {
    /// Create a new client. Pass `None` or empty string for demo mode (no activation key).
    pub fn new(api_key: Option<String>) -> Self {
        Self::with_url(api_key, DEFAULT_API_URL.to_string())
    }

    /// Create a client with a custom API base URL (mainly for testing).
    pub fn with_url(api_key: Option<String>, api_url: String) -> Self {
        let key = api_key.filter(|s| !s.is_empty());
        Self {
            api_key: key,
            api_url,
            client: reqwest::Client::new(),
        }
    }

    /// Login and retrieve license and limit information.
    pub async fn login(&self) -> Result<CodecResult, SteganographyError> {
        let form = self.build_form("login", |f| f);
        self.post_request_codec_result(form).await
    }

    /// Encrypt and hide a message in an image; writes a PNG to `output_image_path`.
    pub async fn encode(
        &self,
        input_image_path: impl AsRef<Path>,
        message_to_hide: &str,
        password: &str,
        output_image_path: impl AsRef<Path>,
    ) -> Result<CodecResult, SteganographyError> {
        let path = input_image_path.as_ref();
        let bytes = tokio::fs::read(path)
            .await
            .map_err(|e| SteganographyError::Api {
                code: errors::INVALID_INPUT,
                error_message: e.to_string(),
                raw: None,
            })?;
        let filename = path
            .file_name()
            .and_then(|n| n.to_str())
            .unwrap_or("image.bin");
        self.encode_bytes(&bytes, filename, message_to_hide, password, output_image_path)
            .await
    }

    /// Same as [`encode`](Self::encode) but reads image bytes you already have in memory.
    pub async fn encode_bytes(
        &self,
        image: &[u8],
        filename_for_upload: &str,
        message_to_hide: &str,
        password: &str,
        output_image_path: impl AsRef<Path>,
    ) -> Result<CodecResult, SteganographyError> {
        if image.is_empty() {
            return Err(SteganographyError::Api {
                code: errors::INVALID_INPUT,
                error_message: "input_image is required".to_string(),
                raw: None,
            });
        }
        if message_to_hide.is_empty() {
            return Err(SteganographyError::Api {
                code: errors::INVALID_INPUT,
                error_message: "message_to_hide is required".to_string(),
                raw: None,
            });
        }
        if password.is_empty() {
            return Err(SteganographyError::Api {
                code: errors::INVALID_INPUT,
                error_message: "password is required".to_string(),
                raw: None,
            });
        }
        let out = output_image_path.as_ref();
        if out.as_os_str().is_empty() {
            return Err(SteganographyError::Api {
                code: errors::INVALID_INPUT,
                error_message: "output_image_path is required".to_string(),
                raw: None,
            });
        }

        let image_part = Part::bytes(image.to_vec())
            .file_name(filename_for_upload.to_string())
            .mime_str("application/octet-stream")
            .map_err(|e| SteganographyError::Api {
                code: errors::UNKNOWN,
                error_message: e.to_string(),
                raw: None,
            })?;

        let form = self.build_form("encode", |f| {
            f.text("message", message_to_hide.to_string())
                .text("password", password.to_string())
                .part("image", image_part)
        });

        let json = self.post_request_json(form).await?;
        let encoded_b64 = json
            .get("encodedImage")
            .and_then(|v| v.as_str())
            .ok_or_else(|| SteganographyError::Api {
                code: errors::UNKNOWN,
                error_message: "Malformed API response: missing encodedImage".to_string(),
                raw: Some(json.clone()),
            })?;

        let decoded = base64::engine::general_purpose::STANDARD
            .decode(encoded_b64)
            .map_err(|e| SteganographyError::Api {
                code: errors::UNKNOWN,
                error_message: e.to_string(),
                raw: Some(json.clone()),
            })?;

        tokio::fs::write(output_image_path.as_ref(), &decoded)
            .await
            .map_err(|e| SteganographyError::Api {
                code: errors::OUTPUT_FILE,
                error_message: e.to_string(),
                raw: Some(json.clone()),
            })?;

        Self::json_to_codec_result(json)
    }

    /// Extract a hidden message from a PNG image.
    pub async fn decode(
        &self,
        input_image_path: impl AsRef<Path>,
        password: &str,
    ) -> Result<CodecResult, SteganographyError> {
        let path = input_image_path.as_ref();
        let bytes = tokio::fs::read(path)
            .await
            .map_err(|e| SteganographyError::Api {
                code: errors::INVALID_INPUT,
                error_message: e.to_string(),
                raw: None,
            })?;
        let filename = path
            .file_name()
            .and_then(|n| n.to_str())
            .unwrap_or("image.png");
        self.decode_bytes(&bytes, filename, password).await
    }

    /// Same as [`decode`](Self::decode) but uses in-memory image bytes.
    pub async fn decode_bytes(
        &self,
        image: &[u8],
        filename_for_upload: &str,
        password: &str,
    ) -> Result<CodecResult, SteganographyError> {
        if image.is_empty() {
            return Err(SteganographyError::Api {
                code: errors::INVALID_INPUT,
                error_message: "input_image is required".to_string(),
                raw: None,
            });
        }
        if password.is_empty() {
            return Err(SteganographyError::Api {
                code: errors::INVALID_INPUT,
                error_message: "password is required".to_string(),
                raw: None,
            });
        }

        let image_part = Part::bytes(image.to_vec())
            .file_name(filename_for_upload.to_string())
            .mime_str("application/octet-stream")
            .map_err(|e| SteganographyError::Api {
                code: errors::UNKNOWN,
                error_message: e.to_string(),
                raw: None,
            })?;

        let form = self.build_form("decode", |f| {
            f.text("password", password.to_string())
                .part("image", image_part)
        });

        self.post_request_codec_result(form).await
    }

    /// Convert a byte count to a human-readable string (e.g. `"1.23 MB"`).
    pub fn convert_size(size_bytes: i64) -> String {
        Self::convert_size_f64(size_bytes as f64)
    }

    fn convert_size_f64(size_bytes: f64) -> String {
        if !size_bytes.is_finite() || size_bytes < 0.0 {
            return "0 bytes".to_string();
        }
        if size_bytes == 0.0 {
            return "0 bytes".to_string();
        }

        const NAMES: &[&str] = &[
            "bytes", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB",
        ];
        let i = ((size_bytes.ln() / 1024_f64.ln()).floor() as usize).min(NAMES.len() - 1);
        let p = 1024_f64.powi(i as i32);
        let s = ((size_bytes / p) * 100.0).round() / 100.0;
        format!("{} {}", s, NAMES[i])
    }

    fn key_field(&self) -> String {
        self.api_key.clone().unwrap_or_default()
    }

    fn build_form(&self, command: &'static str, extend: impl FnOnce(Form) -> Form) -> Form {
        let base = Form::new()
            .text("key", self.key_field())
            .text("command", command.to_string());
        extend(base)
    }

    async fn post_request_json(&self, form: Form) -> Result<serde_json::Value, SteganographyError> {
        let response = self
            .client
            .post(&self.api_url)
            .multipart(form)
            .send()
            .await
            .map_err(|e| SteganographyError::Api {
                code: errors::WEBAPI_CONNECTION,
                error_message: e.to_string(),
                raw: None,
            })?;

        if !response.status().is_success() {
            return Err(SteganographyError::Api {
                code: errors::WEBAPI_CONNECTION,
                error_message: format!("HTTP {}", response.status()),
                raw: None,
            });
        }

        let json: serde_json::Value = response.json().await.map_err(|e| {
            SteganographyError::Api {
                code: errors::WEBAPI_CONNECTION,
                error_message: e.to_string(),
                raw: None,
            }
        })?;

        let err_code = json
            .get("error")
            .and_then(|v| v.as_i64())
            .map(|v| v as i32);

        let Some(code) = err_code else {
            return Err(SteganographyError::Api {
                code: errors::UNKNOWN,
                error_message: "Malformed API response: missing error field".to_string(),
                raw: Some(json),
            });
        };

        if code == errors::SUCCESS {
            return Ok(json);
        }

        let msg = json
            .get("error_message")
            .and_then(|v| v.as_str())
            .unwrap_or("API error")
            .to_string();

        Err(SteganographyError::Api {
            code,
            error_message: msg,
            raw: Some(json),
        })
    }

    async fn post_request_codec_result(
        &self,
        form: Form,
    ) -> Result<CodecResult, SteganographyError> {
        let json = self.post_request_json(form).await?;
        Self::json_to_codec_result(json)
    }

    fn json_to_codec_result(json: serde_json::Value) -> Result<CodecResult, SteganographyError> {
        let license = json
            .get("license")
            .cloned()
            .and_then(|v| serde_json::from_value::<LicenseInfo>(v).ok());
        let limits = json
            .get("limits")
            .cloned()
            .and_then(|v| serde_json::from_value::<LimitsInfo>(v).ok());
        let message = json
            .get("message")
            .and_then(|v| v.as_str())
            .map(|s| s.to_string());

        Ok(CodecResult {
            license,
            limits,
            message,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn convert_size_matches_js_style() {
        assert_eq!(SteganographyOnlineCodec::convert_size(0), "0 bytes");
        assert_eq!(SteganographyOnlineCodec::convert_size(500), "500 bytes");
    }
}