Skip to content

Commit

Permalink
init more (#4)
Browse files Browse the repository at this point in the history
* structure to be usable as a Go dependency
* support release
* add linting, testing
* update key to be hex encoded when used via CLI
* add cobra for CLI
  • Loading branch information
richardjennings authored May 1, 2024
1 parent a0748b7 commit 06a50a5
Show file tree
Hide file tree
Showing 19 changed files with 369 additions and 114 deletions.
15 changes: 8 additions & 7 deletions .github/workflows/example.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name: Build and Deploy Example
name: Example
on:
push:
branches:
- main
- '*'
permissions:
contents: write
pages: write
Expand All @@ -15,13 +15,14 @@ jobs:
- name: Checkout 🛎
uses: actions/checkout@v4

- name: Install and Build 🔧
run: |
make build
mkdir -p build
./precrypt "passphrasewhichneedstobe32bytes!" > build/index.html
- name: Make Build
run: make build

- name: Generate Example
run: mkdir build && ./precrypt --html example/index.html --css example/style.css --js example/index.js --key 329625b9767075c799e90499c59f4e775c0c0ca8c8320b99fc485ba68add025b build/index.html

- name: Deploy 🚀
uses: JamesIves/github-pages-deploy-action@v4
if: github.ref == 'refs/heads/main'
with:
folder: build
20 changes: 20 additions & 0 deletions .github/workflows/lint-go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Lint

on:
push:
pull_request:
types: [opened, reopened]
jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: '1.21'
cache: false
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.54
31 changes: 31 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: goreleaser

on:
push:
tags:
- '*'

permissions:
contents: write

jobs:
go-release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- run: git fetch --force --tags
- uses: actions/setup-go@v4
with:
go-version: stable
- name: Make Build
run: make build
- uses: goreleaser/goreleaser-action@v4
with:
distribution: goreleaser
version: latest
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

18 changes: 18 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Tests

on:
push:
pull_request:
types: [opened, reopened]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 2
- uses: actions/setup-go@v3
with:
go-version: '1.21'
- name: Run tests
run: go test -v ./...
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/node_modules
/dist
/.idea
/precrypt
/index.html
/index.html
24 changes: 18 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,29 @@
PreCrypt generates a HTML page which can self load embedded AES GCM encrypted HTML, CSS, Javascript when a correct
passcode is entered.

The demo example is available on [Github Pages](https://richardjennings.github.io/precrypt/).
An example is available on [Github Pages](https://richardjennings.github.io/precrypt/).

## Status

Work in progress currently only supporting embedding content from the `example` directory.
## CLI Usage

## Example
Key is hex encoded and is generated if not supplied.

```
go run main.go passphrasewhichneedstobe32bytes! > index.html
open index.html
precrypt --html example/index.html --css example/style.css --js example/index.js --key 329625b9767075c799e90499c59f4e775c0c0ca8c8320b99fc485ba68add025b index.html
```

## Library Usage

```
key := precrypt.Render(precrypt.RenderOptions{
HtmlFiles: []string{"example/index.html"},
CssFiles: []string{"example/style.css"},
JsFiles: []string{"example/index.js"},
Key: []byte{"passphrasewhichneedstobe32bytes!"},
Out: os.Stdout,
}
```



73 changes: 73 additions & 0 deletions cmd/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package cmd

import (
"crypto/rand"
"encoding/hex"
"fmt"
"github.com/richardjennings/precrypt/pkg/precrypt"
"github.com/spf13/cobra"
"os"
)

var htmlFiles []string
var cssFiles []string
var jsFiles []string
var key string

var build = &cobra.Command{
Use: "",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
var err error
out := args[0]
fh, err := os.OpenFile(out, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
defer func() { _ = fh.Close() }()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
opts := precrypt.RenderOptions{
HtmlFiles: htmlFiles,
CssFiles: cssFiles,
JsFiles: jsFiles,
Out: fh,
}
if len(key) > 0 {
opts.Key, err = hex.DecodeString(key)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
if len(key) == 0 {
// generate key
bytes := make([]byte, 32)
if _, err := rand.Read(bytes); err != nil {
fmt.Println(err)
os.Exit(1)
}
opts.Key = make([]byte, hex.EncodedLen(len(bytes)))
hex.Encode(opts.Key, bytes)
fmt.Println(string(opts.Key))
}
if err := precrypt.Render(opts); err != nil {
fmt.Println(err)
os.Exit(1)
}
},
}

func init() {
build.Flags().StringArrayVar(&htmlFiles, "html", []string{}, "--html <file1.html>,<file2.html>")
build.Flags().StringArrayVar(&cssFiles, "css", []string{}, "--css <file1.css>,<file2.css>")
build.Flags().StringArrayVar(&jsFiles, "js", []string{}, "--js <file1.js>,<file2.js>")
build.Flags().StringVar(&key, "key", "", "--key 32byteslong")
}

func Execute() {
err := build.Execute()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
module github.com/richardjennings/precrypt

go 1.21

require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/cobra v1.8.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
46 changes: 0 additions & 46 deletions internal/precrypt/precrypt.go

This file was deleted.

49 changes: 2 additions & 47 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,54 +1,9 @@
package main

import (
_ "embed"
"encoding/json"
"fmt"
"github.com/richardjennings/precrypt/internal/precrypt"
"os"
"text/template"
"github.com/richardjennings/precrypt/cmd"
)

//go:embed template/index.html
var indexTemplate string

//go:embed dist/main.js
var loaderJs string

//go:embed dist/style.css
var loaderCss string

//go:embed example/index.html
var exampleHtml []byte

//go:embed example/style.css
var exampleCss []byte

//go:embed example/index.js
var exampleJs []byte

func main() {
key := os.Args[1]
htmlEnc, err := precrypt.Encrypt(exampleHtml, []byte(key))
e(err)
cssEnc, err := precrypt.Encrypt(exampleCss, []byte(key))
e(err)
jsEnc, err := precrypt.Encrypt(exampleJs, []byte(key))
e(err)
tmpl := template.Must(template.New("page").Parse(indexTemplate))
data, err := json.Marshal(map[string]interface{}{
"a": []string{string(htmlEnc)},
"b": []string{string(cssEnc)},
"c": []string{string(jsEnc)},
})
e(err)
err = tmpl.Execute(os.Stdout, map[string]interface{}{"data": string(data), "js": loaderJs, "css": loaderCss})
e(err)
}

func e(err error) {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
cmd.Execute()
}
File renamed without changes.
Loading

0 comments on commit 06a50a5

Please sign in to comment.