Encrypt Module

The encrypt module allows encrypting or protecting partial contents.

Module
github.com/hugomods/encrypt
GitHub Stars Used By Used By Used By Used By

How it Works?

This module ships with a shortcode and a CLI tool, to walk though the Hugo generated files and encrypt the content in AES-256-GCM.

graph TD;
  content[Protect content via shortcode `encrypt`]-->build;
  build[Build site, i.e. `hugo --minify --gc ...`]-->encrypt[Encrypt content via `$HOME/go/bin/encrypt`];

The difference between this and normal build is that, you need to combine the CLI tool with Hugo, i.e.

1hugo && $HOME/go/bin/encrypt

Features

  • Partial encryption, protect the key part, SEO friendly.
  • Remember password to auto decrypt.

Notes

There are some stuff you should pay attention to.

  • Keep your repo (source files) private, since the raw password and content are plaintext.
  • Execute the CLI tool after building the site, otherwise the generated files are not encrypted.

Installation

To get the CLI tool, you can either download from the releases or install from source.

Install from Source

1go install github.com/hugomods/encrypt/commands/encrypt@latest

Then you’re able to access the command

1$HOME/go/bin/encrypt

Integrate with Theme

This section is for developers to integrate this module into their themes.

Import the Module

hugo.yaml

1module:
2  imports:
3  - path: github.com/hugomods/encrypt

hugo.toml

1[module]
2[[module.imports]]
3    path = 'github.com/hugomods/encrypt'

hugo.json

1{
2   "module": {
3      "imports": [
4         {
5            "path": "github.com/hugomods/encrypt"
6         }
7      ]
8   }
9}

Import the CSS

Please skip this step if your theme supports HugoPress, such as HB Framework themes.

1{{ partial "encrypt/assets/css" . }}

Import the JS

Please skip this step if your theme supports HugoPress, such as HB Framework themes.

1{{ partial "encrypt/assets/js" . }}

Configuration

ParameterTypeRequiredDefaultDescription
passwordstringY-The default password.
storagestringYsessionsession (SessionStorage) or local (LocalStorage).

hugo.yaml

1params:
2  encrypt:
3    password: DEFAULT_PASSWORD
4    storage: session

hugo.toml

1[params]
2  [params.encrypt]
3    password = 'DEFAULT_PASSWORD'
4    storage = 'session'

hugo.json

1{
2   "params": {
3      "encrypt": {
4         "password": "DEFAULT_PASSWORD",
5         "storage": "session"
6      }
7   }
8}

Usage

This section shows users how to protect their contents, please make sure you’ve imported the module.

Default password: hugomods.com.

Encrypt Raw Content

1{{< encrypt >}}
2Hello World!
3{{< /encrypt >}}

The following content is protected.

453d3438e50f16d4-45a51dce6ce396cb008a3dd1-32742a51f02b682d45ffac678a514cdad027ca3cd0af55019fad957f538b6148c586ad2978b26aa3820316e1cbb776603edff38362bb28b84c792b8faec00abf951b1458b39bcf6e7b6a2131e3bc68ac4d010bccf11a13104d822771b0952054ccf5d87d49562e6c38eedd83d2b7

Encrypt Markdown Content

1{{% encrypt %}}
2**Hello World!**
3{{% /encrypt %}}

The following content is protected.

2c11dd885edb3671-79892a0422cec259395e5593-410496b73ad75ae3cb0190d9277a9224c515399f756d5cce30073fa90ae592fd1a385cbdba4c8d1bd56c62a485e38d073c02225e10f1df3775d45c9a83f387781e549858e9032fe5a3bf25788f076ea130778e387f73e920722363eb845a911cec37f9a5d6937557089c75e2fd955a040973af43e22c2e6ac8566ac290f5e54d0c6e9fcf59f9

Encrypt Content with Specified Password

1{{% encrypt foo %}}
2**Bar!**
3{{% /encrypt %}}

The following content is protected.

8343859e700c2d64-45a1ef28a7468912dfdfdd7d-ecbdaeb20371abcaee586a1c6f81e45121883efca4ea5a10183e7cc387265962ba8dab8f2bdb933508652ef08f69f53e5715842cb66049d2bb8b659624b3dd16861b26c9df7dd4d1656d1a753098c2a77ab0fa874a1cd7f055e801c0f8871edaa73fbbe3c59de0a4c7d60536df3551aa57974e4f9cf97d96a1ef5daef07c

Testing

Since the command must be executed after building the site, that is, it won’t work with the hugo server.

Firstly, build the site and encrypt the content.

1hugo -b http://localhost:8081 && $HOME/go/bin/encrypt

And then setup a HTTP server to serve the encrypted contents, such as http-server and PHP built-in server.

HTTP Server

To get the http-server by executing npm install --global http-server.

1http-server -p 8081 public

PHP Built-in Server

If you’ve PHP installation on your environment.

1php -S 0.0.0.0:8081 -t public

Deployment

Credit

It’s a fork of Hugo Encrypt, and port it for Hugo modules.