Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce _substitute_pixel to simplify everything #78

Merged
merged 5 commits into from
Apr 29, 2022
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Unreleased

## Optimisations

- Added `_substitute_pixel` to perform substitution for a single pixel, and replaced the current implementation of `substitution` algorithm with the same ([#78](https://github.com/Saransh-cpp/ChaoticEncryption.jl/pull/78), @Saransh-cpp)

# ChaoticEncryption v0.3.1

## Optimisations
Expand Down
1 change: 0 additions & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ makedocs(
],
"Developer Documentation" => [
"Encryption/decryption algorithms" => "devdocs/algorithms.md",
"Utility functions" => "devdocs/utils.md"
]
]
)
Expand Down
13 changes: 12 additions & 1 deletion docs/src/devdocs/algorithms.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,22 @@

Developer documentation for encryption and decryption algorithms.

These lower level APIs can also be used by the users, but are not recommended
for beginners.

```@docs
ChaoticEncryption._substitution(
image::Union{String,Array{RGB{N0f8},2}},
keys::Vector{Int64},
type::Symbol;
path_for_result::String="./encrypted.png"
path_for_result::String="./encrypted.png",
inplace=false,
)
```

```@docs
ChaoticEncryption._substitute_pixel(
pixel::RGB,
key::Int64
)
```
10 changes: 0 additions & 10 deletions docs/src/devdocs/utils.md

This file was deleted.

41 changes: 26 additions & 15 deletions src/substitution.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using Images

include("./utils.jl")


"""
_substitution(image, keys, type; path_for_result="./encrypted.png")
Expand Down Expand Up @@ -46,21 +44,11 @@ function _substitution(
@info "DECRYPTING"
end

# generate arrays for r, g, b values of each pixel
r = Array{Int64}(undef, height, width)
g = Array{Int64}(undef, height, width)
b = Array{Int64}(undef, height, width)

# resize keys for broadcasting
# reshape keys for broadcasting
keys = reshape(keys, height, width)

# collect all r, g, b values without loop
@. r = trunc(Int, _redify(image) * 255)
@. g = trunc(Int, _greenify(image) * 255)
@. b = trunc(Int, _blueify(image) * 255)

# XOR all r, g, b values and replace the values in image
@. image = _imageify((r ⊻ keys) / 255, (g ⊻ keys) / 255, (b ⊻ keys) / 255)
# substitute all pixels in one go
@. image = _substitute_pixel(image, keys)

if type == :encrypt
@info "ENCRYPTED"
Expand All @@ -73,6 +61,29 @@ function _substitution(
end


"""
_substitute_pixel(pixel::RGB, key::Int64)

Returns the pixel after XORing the R, G, and B values with the key.
Specifically developed to return an `Array` (or the complete image)
of XORed RGB values in one go.

See [`_substitution`](@ref) for more details.

# Arguments
- `pixel::RGB`: Pixel value with r, g, and b components.
- `key::Int64`: The key.

# Returns
- `pixel::RGB`: Substituted pixel.
"""
_substitute_pixel(pixel::RGB, key::Int64) = RGB(
(trunc(Int, pixel.r * 255) ⊻ key) / 255,
(trunc(Int, pixel.g * 255) ⊻ key) / 255,
(trunc(Int, pixel.b * 255) ⊻ key) / 255
)


"""
substitution_encryption(image, keys; path_for_result="./encrypted.png")

Expand Down
72 changes: 0 additions & 72 deletions src/utils.jl

This file was deleted.