Skip to content

Commit

Permalink
chore: rename some namings and add some profilings to the ci (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
supercaracal authored Feb 12, 2024
1 parent 0e78726 commit ae445ef
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 26 deletions.
45 changes: 37 additions & 8 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,54 @@ jobs:
defaults:
run:
shell: bash
strategy:
fail-fast: false
matrix:
task:
- lint
- test
- bench
steps:
- name: Check out code
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version-file: go.mod
check-latest: true
cache: true

- name: Download modules
run: go mod download

- name: Lint
run: make lint

- name: Test
run: make test
- name: Run
run: make ${{ matrix.task }}
profiling:
name: Profiling
timeout-minutes: 5
runs-on: ubuntu-latest
defaults:
run:
shell: bash
strategy:
fail-fast: false
matrix:
package: ["pgpasswd"]
type: ["cpu", "mem"]
steps:
- name: Check out code
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version-file: go.mod
check-latest: true
cache: true
- name: Download modules
run: go mod download
- name: Run
run: make prof
env:
PKG: ${{ matrix.package }}
TYPE: ${{ matrix.type }}
feature:
name: Feature
timeout-minutes: 5
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/cmd/term/encrypt
/cmd/debug/server
*.test
*.out
19 changes: 17 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
MAKEFLAGS += --warn-undefined-variables
SHELL ?= /bin/bash -euo pipefail
GOOS ?= $(shell go env GOOS)
GOARCH ?= $(shell go env GOARCH)
CGO_ENABLED ?= $(shell go env CGO_ENABLED)
Expand All @@ -8,6 +9,8 @@ define go-build
go build -ldflags="-s -w" -trimpath -o $@ $^
endef

all: build test lint

build: term wasm

term: cmd/term/encrypt
Expand All @@ -21,8 +24,20 @@ test:
lint:
@go vet ./...

bench:
@go test -bench=. -benchmem -run=NONE ./...

prof: PKG ?= pgpasswd
prof: TYPE ?= mem
prof:
@if [ -z "${PKG}" ]; then echo 'empty variable: PKG'; exit 1; fi
@if [ -z "${TYPE}" ]; then echo 'empty variable: TYPE'; exit 1; fi
@if [ ! -d "./pkg/${PKG}" ]; then echo 'package not found: ${PKG}'; exit 1; fi
@go test -bench=. -run=NONE -${TYPE}profile=${TYPE}.out ./pkg/${PKG}
@go tool pprof -text -nodecount=10 ${PKG}.test ${TYPE}.out

clean:
@rm -f cmd/term/encrypt cmd/debug/server
@rm -f cmd/term/encrypt cmd/debug/server *.test *.out

cmd/term/encrypt: cmd/term/main.go
$(call go-build)
Expand All @@ -39,5 +54,5 @@ docs/encrypt.wasm: cmd/wasm/main.go
docs/wasm_exec.js: $(shell go env GOROOT)/misc/wasm/wasm_exec.js
@cp $^ $@

.PHONY: build term wasm test lint clean \
.PHONY: build term wasm test lint bench prof clean \
cmd/term/encrypt docs/encrypt.wasm
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
![](https://github.com/supercaracal/scram-sha-256/workflows/Test/badge.svg?branch=master)
![](https://github.com/supercaracal/scram-sha-256/workflows/Release/badge.svg)

SCRAM-SHA-256
scram-sha-256
===============================================================================

This is a password-encryption tool for PostgreSQL with [SCRAM-SHA-256](https://www.postgresql.org/docs/current/auth-password.html).
This is a password-encryption tool for PostgreSQL with [scram-sha-256](https://www.postgresql.org/docs/current/auth-password.html).
You can encrypt your raw password by the command-line tool or the [GitHub Pages](https://supercaracal.github.io/scram-sha-256/).

## Installation
Expand Down
15 changes: 7 additions & 8 deletions cmd/term/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ func readViaTerminal(fd int) ([]byte, error) {
return passwd, nil
}

func readViaPipe() ([]byte, error) {
func readViaPipeline() ([]byte, error) {
r := bufio.NewReader(os.Stdin)
passwd, err := r.ReadBytes('\n')
if err == io.EOF {
return passwd, nil
} else if err != nil {
}
if err != nil {
return nil, err
}
return passwd[0 : len(passwd)-1], nil
Expand All @@ -36,28 +37,26 @@ func getRawPassword(args []string) ([]byte, error) {
if len(args) > 1 {
return []byte(args[1]), nil
}

fd := int(syscall.Stdin)
if terminal.IsTerminal(fd) {
return readViaTerminal(fd)
}

return readViaPipe()
return readViaPipeline()
}

func main() {
rawPassword, err := getRawPassword(os.Args)
raw, err := getRawPassword(os.Args)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

if len(rawPassword) == 0 {
if len(raw) == 0 {
fmt.Println("empty password")
os.Exit(1)
}

encrypted, err := pgpasswd.Encrypt(rawPassword)
encrypted, err := pgpasswd.Encrypt(raw)
if err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down
Binary file modified docs/encrypt.wasm
Binary file not shown.
13 changes: 7 additions & 6 deletions pkg/pgpasswd/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,14 @@ func getSHA256Sum(key []byte) []byte {
return h.Sum(nil)
}

func encryptPassword(rawPassword, salt []byte, iter, keyLen int) string {
digestKey := pbkdf2.Key(rawPassword, salt, iter, keyLen, sha256.New)
func encrypt(raw, salt []byte, iter, keyLen int) string {
digestKey := pbkdf2.Key(raw, salt, iter, keyLen, sha256.New)
clientKey := getHMACSum(digestKey, clientRawKey)
storedKey := getSHA256Sum(clientKey)
serverKey := getHMACSum(digestKey, serverRawKey)

return fmt.Sprintf("SCRAM-SHA-256$%d:%s$%s:%s",
return fmt.Sprintf(
"SCRAM-SHA-256$%d:%s$%s:%s",
iter,
string(encodeB64(salt)),
string(encodeB64(storedKey)),
Expand All @@ -74,8 +75,8 @@ func encryptPassword(rawPassword, salt []byte, iter, keyLen int) string {
}

// Encrypt encrypts a raw password with scram-sha-256
func Encrypt(rawPassword []byte) (string, error) {
if rawPassword == nil || len(rawPassword) == 0 {
func Encrypt(raw []byte) (string, error) {
if len(raw) == 0 {
return "", nil
}

Expand All @@ -84,5 +85,5 @@ func Encrypt(rawPassword []byte) (string, error) {
return "", err
}

return encryptPassword(rawPassword, salt, iterationCnt, digestLen), nil
return encrypt(raw, salt, iterationCnt, digestLen), nil
}
14 changes: 14 additions & 0 deletions pkg/pgpasswd/crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ import (
"testing"
)

var (
dummyPassword = []byte("dummyblahfoobarbaztest")
)

func TestEncrypt(t *testing.T) {
cases := []struct {
raw []byte
err error
}{
{[]byte("foo"), nil},
{[]byte(""), nil},
{[]byte{}, nil},
{nil, nil},
}

Expand All @@ -22,3 +27,12 @@ func TestEncrypt(t *testing.T) {
}
}
}

func BenchmarkEncrypt(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, err := Encrypt(dummyPassword); err != nil {
b.Fatal(err)
}
}
}

0 comments on commit ae445ef

Please sign in to comment.