Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/foxhound-systems/hs-obfuscate
Library for easy obfuscation and deobfuscation of integer IDs in custom data types
https://github.com/foxhound-systems/hs-obfuscate
hashids haskell obfuscation
Last synced: 18 days ago
JSON representation
Library for easy obfuscation and deobfuscation of integer IDs in custom data types
- Host: GitHub
- URL: https://github.com/foxhound-systems/hs-obfuscate
- Owner: foxhound-systems
- License: bsd-3-clause
- Created: 2020-10-22T20:59:34.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2025-01-08T18:22:01.000Z (about 1 month ago)
- Last Synced: 2025-01-08T19:32:17.753Z (about 1 month ago)
- Topics: hashids, haskell, obfuscation
- Language: Haskell
- Homepage:
- Size: 14.6 KB
- Stars: 5
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
Obfuscate
=========This library makes it easy to use obfuscation in your application code, allowing you to obscure numeric ids before sending them to a client (such as in a JSON API or other web service). This library is backed by [`foxhound-systems/hashids-st`](https://github.com/foxhound-systems/hashids-st), which itself is an implementation of the [Hashids.org](https://hashids.org/) obfuscation interface.
## Implementation
The `Web.Obfuscate` module provides `CanObfuscate` and `CanDeobfuscate` typeclasses along with several default instances for basic data types. In addition to manually defining your own instances, `Web.Obfuscate.TH` will derive a sensible default using `deriveObfuscate`.
## Example Usage
### Basic Usage
```haskell
import Web.Obfuscate
import Hashids...
hashidsContext :: HashidsContext
hashidsContext = ctx
where
(Right ctx) = mkHashidsContext "test-salt-please-ignore" 7 defaultAlphabetgetUser :: Obfuscated UserId -> IO (User)
getUser obfuscatedUserId =
maybeUserId <- deobfuscate hashidsContext obfuscatedUserId
case maybeUserId of
Just userId ->
fetchUserById userIdNothing ->
throwIO err400```
### Deriving Obfuscation Instances with Template Haskell
```haskell
{-# LANGUAGE TemplateHaskell #-}module ForumResponse
whereimport Web.Obfuscate
import Web.Obfuscate.TH
import qualified Data.Text as T-- Another module that defines another custom obfuscatable type
import ForumAdministratordata ForumResponse = ForumResponse
{ frForumId :: ForumId
, frName :: Text
, frDescription :: Text
, frCreator :: ForumAdministrator
, frAdministrators :: [ForumAdministrator]
}$(deriveObfuscate defaultObfuscationOptions ''ForumResponse)
```The above Template Haskell code will generate something like the following:
```haskell
data ObfuscatedForumResponse = ObfuscatedForumResponse
{ obfrForumId :: Obfuscated ForumId
, obfrName :: Text
, obfrDescription :: Text
, obfrCreator :: Obfuscated ForumAdministrator
, obfrAdministrators :: Obfuscated [ForumAdministrator]
}type instance Obfuscated ForumResponse = ObfuscatedForumResponse
instance CanObfuscate ForumResponse where
obfuscate ctx forumResponse =
ObfuscatedForumResponse
{ obfrForumId = obfuscate ctx $ frForumId forumResponse
, obfrName = frName forumResponse
, obfrDescription = frDescription forumResponse
, obfrCreator = obfuscate ctx $ frCreator forumResponse
, obfrAdministrators = obfuscate ctx $ frAdministrators forumResponse
}instance CanDeobfuscateForumResponse where
deobfuscate ctx obfuscatedForumResponse = do
forumId <- deobfuscate ctx $ obfrForumId obfuscatedForumResponse
creator <- deobfuscate ctx $ obfrCreator obfuscatedForumResponse
administrators <- deobfuscate ctx $ obfrAdministrators obfuscatedForumResponse
pure $ ForumResponse
{ frForumId = forumId
, frName = obfrName obfuscatedForumResponse
, frDescription = obfrDescription obfuscatedForumResponse
, frCreator = creator
, frAdministrators = administrators
}
```## Development
Development of this library is done using Nix. With `nix` installed, following command to start a ghcid session in an isolated environment and run the reloading tests:
```
nix-shell --run "make tests-watch"
```Run `nix-build` to perform a full build of the library.
## License
See the [LICENSE](https://github.com/foxhound-systems/hs-obfuscate/blob/master/LICENSE) file.