├── .github └── workflows │ └── build.yml ├── LICENSE ├── README.md ├── src └── steganography.nim ├── steganography.nimble └── tests ├── steganographyLogo.png ├── steganographyLogoEncoded.png └── test.nim /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: Github Actions 2 | on: [push, pull_request] 3 | jobs: 4 | build: 5 | strategy: 6 | fail-fast: false 7 | matrix: 8 | os: [ubuntu-latest, windows-latest] 9 | 10 | runs-on: ${{ matrix.os }} 11 | 12 | steps: 13 | - uses: actions/checkout@v2 14 | - uses: jiro4989/setup-nim-action@v1 15 | - run: nimble test -y 16 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2021 Andre von Houck 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![Steganography Logo](tests/steganographyLogoEncoded.png) 2 | 3 | # Steganography - hide data inside an image. 4 | 5 | `nimble install steganography` 6 | 7 | ![Github Actions](https://github.com/treeform/steganography/workflows/Github%20Actions/badge.svg) 8 | 9 | [API reference](https://nimdocs.com/treeform/steganography) 10 | 11 | ## About 12 | 13 | * Has uses in cryptography: https://en.wikipedia.org/wiki/Steganography 14 | * Can be used in games to transfer user creations: https://nedbatchelder.com/blog/200806/spore_creature_creator_and_steganography.html 15 | * Or just store extra meta infromation in PNG files: https://gamedev.stackexchange.com/questions/72760/how-can-i-store-game-metadata-in-a-png-file 16 | 17 | ## Encode 18 | 19 | This is how you would encode an image with a secret message: 20 | 21 | ```nim 22 | var image = readImage("tests/steganographyLogo.png") 23 | encodeMessage(image, "Hello world this is really cool") 24 | image.writeFile("tests/steganographyLogoEncoded.png") 25 | ``` 26 | 27 | This is how you would decode an image that contains a secret message: 28 | 29 | ``` nim 30 | var image = readImage("tests/steganographyLogoEncoded.png") 31 | doAssert decodeMessage(image) == "Hello world this is really cool" 32 | ``` 33 | -------------------------------------------------------------------------------- /src/steganography.nim: -------------------------------------------------------------------------------- 1 | import pixie 2 | 3 | proc encodeMessage*(image: Image, data: string) = 4 | ## Hide data inside an image 5 | for i in 0..data.len: 6 | var dataByte: uint8 7 | if i < data.len: 8 | dataByte = uint8(data[i]) 9 | var 10 | c1 = image.data[i*2+0] 11 | c2 = image.data[i*2+1] 12 | c1.r = (c1.r and 0b11111000) + (dataByte and 0b00000001) shr 0 13 | c1.g = (c1.g and 0b11111100) + (dataByte and 0b0000110) shr 1 14 | c1.b = (c1.b and 0b11111000) + (dataByte and 0b0001000) shr 3 15 | c1.a = 255 16 | c2.r = (c2.r and 0b11111000) + (dataByte and 0b00110000) shr 4 17 | c2.g = (c2.g and 0b11111100) + (dataByte and 0b01000000) shr 6 18 | c2.b = (c2.b and 0b11111000) + (dataByte and 0b10000000) shr 7 19 | c2.a = 255 20 | image.data[i*2+0] = c1 21 | image.data[i*2+1] = c2 22 | 23 | proc decodeMessage*(image: Image): string = 24 | ## Extract hidden data in the image 25 | result = "" 26 | for i in 0..<(image.data.len div 4): 27 | var dataByte: uint8 28 | let 29 | c1 = image.data[i*2+0] 30 | c2 = image.data[i*2+1] 31 | dataByte += (c1.r and 0b1) shl 0 32 | dataByte += (c1.g and 0b11) shl 1 33 | dataByte += (c1.b and 0b1) shl 3 34 | dataByte += (c2.r and 0b11) shl 4 35 | dataByte += (c2.g and 0b1) shl 6 36 | dataByte += (c2.b and 0b1) shl 7 37 | 38 | if dataByte == 0: 39 | break 40 | result.add char(dataByte) 41 | -------------------------------------------------------------------------------- /steganography.nimble: -------------------------------------------------------------------------------- 1 | # Package 2 | 3 | version = "0.2.0" 4 | author = "Andre von Houck" 5 | description = "Steganography - hide data inside an image." 6 | license = "MIT" 7 | 8 | srcDir = "src" 9 | 10 | # Deps 11 | 12 | requires "nim >= 1.4.0" 13 | requires "pixie >= 4.3.0" 14 | -------------------------------------------------------------------------------- /tests/steganographyLogo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/treeform/steganography/edded6d5bec7f0f8d9c09074e1da1376f850f58d/tests/steganographyLogo.png -------------------------------------------------------------------------------- /tests/steganographyLogoEncoded.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/treeform/steganography/edded6d5bec7f0f8d9c09074e1da1376f850f58d/tests/steganographyLogoEncoded.png -------------------------------------------------------------------------------- /tests/test.nim: -------------------------------------------------------------------------------- 1 | import ../src/steganography, pixie 2 | 3 | block: 4 | var image = readImage("tests/steganographyLogo.png") 5 | encodeMessage(image, "Hello world this is really cool") 6 | image.writeFile("tests/steganographyLogoEncoded.png") 7 | 8 | block: 9 | var image = readImage("tests/steganographyLogoEncoded.png") 10 | doAssert decodeMessage(image) == "Hello world this is really cool" 11 | --------------------------------------------------------------------------------