Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,13 @@ homepage = "https://github.com/daniel-e/captcha"
repository = "https://github.com/daniel-e/captcha"

[dependencies]
image = { version = "0.24.2", default-features = false, features = ["png"] }
image = { version = "0.25.2", default-features = false, features = ["png"] }
rand = "0.8.5"
serde_json = "1.0"
base64 = "0.13"
base64 = "0.22.1"
lodepng = "3.6.1"
hound = { version = "3.4", optional = true }

[dev-dependencies]
time = "0.3.9"

[features]
default = ["audio"]
audio = ["hound"]
12 changes: 5 additions & 7 deletions examples/perf.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
extern crate captcha;
extern crate time;

use captcha::{gen, Difficulty};
use std::thread;
use time::Instant;
use std::{thread, time::Instant};

fn main() {
let n = 500;
Expand All @@ -17,7 +15,7 @@ fn main() {
for _ in 0..n {
gen(Difficulty::Easy).as_tuple();
}
println!("done {:?} ms", b.elapsed().whole_milliseconds());
println!("done {:?} ms", b.elapsed().as_millis());
});
threads.push(h);
}
Expand All @@ -28,13 +26,13 @@ fn main() {

let d = b.elapsed();
println!("n : {}", n * nthreads);
println!("time in ms total : {}", d.whole_milliseconds());
println!("time in ms total : {}", d.as_millis());
println!(
"time in ms per captcha: {}",
d.whole_milliseconds() as f64 / (n * nthreads) as f64
d.as_millis() as f64 / (n * nthreads) as f64
);
println!(
"#captchs per second : {}",
(n * nthreads * 1000) / (d.whole_milliseconds() as i64)
(n * nthreads * 1000) / (d.as_millis() as i64)
);
}
4 changes: 2 additions & 2 deletions src/audio/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[cfg(feature = "audio")]
use base64::decode;
use base64::{engine::general_purpose, Engine};
#[cfg(feature = "audio")]
use hound::Result;
#[cfg(feature = "audio")]
Expand Down Expand Up @@ -29,7 +29,7 @@ impl Audio {
pub fn as_wav(&self, letter: char) -> Option<Vec<u8>> {
match self.data.get(&letter) {
None => None,
Some(s) => match decode(s) {
Some(s) => match general_purpose::STANDARD.decode(s) {
Err(_) => None,
Ok(v) => match Audio::add_noise(v) {
Ok(v) => Some(v),
Expand Down
2 changes: 1 addition & 1 deletion src/filters/cow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl Cow {
for &(x, y) in v {
let mut p = i.get_pixel(x, y);
p.invert();
i.put_pixel(x as u32, y as u32, p);
i.put_pixel(x, y, p);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/fonts/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use base64::decode;
use base64::{engine::general_purpose, Engine};
use serde_json;
use std::collections::HashMap;

Expand All @@ -11,7 +11,7 @@ pub trait Font {
fn png(&self, letter: char) -> Option<Vec<u8>> {
match self.png_as_base64(letter) {
None => None,
Some(s) => match decode(s) {
Some(s) => match general_purpose::STANDARD.decode(s) {
Err(_) => None,
Ok(v) => Some(v),
},
Expand Down
10 changes: 4 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ mod samples;

pub use samples::{by_name, gen, CaptchaName, Difficulty};

use base64::{engine::general_purpose, Engine};
use filters::Filter;
use fonts::{Default, Font};
use images::{Image, Pixl};
Expand Down Expand Up @@ -113,7 +114,6 @@ pub struct RngCaptcha<T> {
}

impl<T: rand::Rng + rand::RngCore> RngCaptcha<T> {

pub fn from_rng(rng: T) -> RngCaptcha<T> {
// TODO fixed width + height
let w = 400;
Expand Down Expand Up @@ -309,18 +309,16 @@ impl<T: rand::Rng + rand::RngCore> RngCaptcha<T> {
}

pub fn as_base64(&self) -> Option<String> {
self.as_png().map(base64::encode)
self.as_png()
.map(|png| general_purpose::STANDARD.encode(&png))
}

/// Returns a tuple which contains the characters that have been added to this CAPTCHA
/// as a string and the image encoded as a PNG.
///
/// Returns `None` on error.
pub fn as_tuple(&self) -> Option<(String, Vec<u8>)> {
match self.as_png() {
None => None,
Some(p) => Some((self.chars_as_string(), p)),
}
self.as_png().map(|p| (self.chars_as_string(), p))
}

/// Returns the supported characters of the current font.
Expand Down