Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Halfi committed Apr 6, 2022
0 parents commit 5c75ccf
Show file tree
Hide file tree
Showing 27 changed files with 1,667 additions and 0 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: "CodeQL"

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
schedule:
- cron: '0 19 * * 0'

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write

strategy:
fail-fast: false
matrix:
language: [ 'go' ]

steps:
- name: Checkout repository
uses: actions/checkout@v3

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}

- name: Autobuild
uses: github/codeql-action/autobuild@v2

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
42 changes: 42 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Lint

on:
push:

jobs:
lint:
name: Lint
runs-on: ubuntu-latest
strategy:
matrix:
go: [1.16, 1.17, 1.18]
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
fetch-depth: 2

- name: Setup go
id: go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go }}

- name: Cache go
uses: actions/cache@v2
with:
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-v${{ matrix.go }}-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Golangci lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.45.2
skip-pkg-cache: true
skip-build-cache: true
only-new-issues: true
args: -v ./...
51 changes: 51 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: Tests

on:
push:
schedule:
- cron: '0 19 * * 0'

jobs:
test:
name: Tests
runs-on: ubuntu-latest
strategy:
matrix:
go: [1.16, 1.17, 1.18]
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
fetch-depth: 2

- name: Setup go
id: go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go }}

- name: Cache go
uses: actions/cache@v2
with:
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-v${{ matrix.go }}-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Run tests
id: test
run: |
make test
echo "::set-output name=coverage::$(make coverage)"
- name: Create Coverage Badge
uses: schneegans/[email protected]
with:
auth: ${{ secrets.GIST_SECRET }}
gistID: cce912d48b587ba656b45a0cba34510b
filename: pow-hc-test-coverage.json
label: Coverage
message: ${{ steps.test.outputs.coverage }}
color: green
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea
.DS_Store

coverage.txt
46 changes: 46 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# This file contains all available configuration options
# with their default values.

# options for analysis running
run:
# timeout for analysis, e.g. 30s, 5m, default is 1m
timeout: 5m

# include test files or not, default is true
tests: false

linters:
disable:
- scopelint
enable:
- errcheck
- goimports
- gofmt
- revive
- exportloopref
- prealloc
- lll
- whitespace
- unconvert
- goconst
- staticcheck
- govet
- gocritic
presets:
- bugs
- unused

linters-settings:
lll:
line-length: 180
revive:
ignore-generated-header: true
rules:
- name: unexported-return
disabled: true

issues:
exclude-rules:
- linters:
- lll
source: "^//go:generate "
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Kirill Rodin

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:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

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.
22 changes: 22 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.PHONY: install
install:
@go mod tidy
@go install \
github.com/abice/go-enum \
github.com/golang/mock/mockgen

.PHONY: gen
gen: install
@go generate ./...

.PHONY: test
test: gen
@go test $$(go list ./... | grep -v /mock) -coverprofile=coverage.txt -covermode=atomic -timeout 60s

.PHONY: coverage
coverage:
@make test | tr -d '\n' | sed -e 's/.*coverage:\(.*\)of statements.*/\1/' | tr -d '\n '

.PHONY: lint
lint:
@golangci-lint --timeout 10m0s run
82 changes: 82 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Hashcash

[![GoDoc](https://img.shields.io/badge/reference-007d9c.svg?logo=go&logoColor=white&label=doc)](https://pkg.go.dev/github.com/PoW-HC/hashcash)
[![Go Report Card](https://goreportcard.com/badge/github.com/PoW-HC/hashcash)](https://goreportcard.com/report/github.com/PoW-HC/hashcash)
[![Lint](https://github.com/PoW-HC/hashcash/actions/workflows/lint.yml/badge.svg)](https://github.com/PoW-HC/hashcash/actions/workflows/lint.yml)
[![Tests](https://github.com/PoW-HC/hashcash/actions/workflows/tests.yml/badge.svg)](https://github.com/PoW-HC/hashcash/actions/workflows/tests.yml)
![Coverage](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/halfi/cce912d48b587ba656b45a0cba34510b/raw/pow-hc-test-coverage.json)
[![Code quality](https://github.com/PoW-HC/hashcash/actions/workflows/codeql.yml/badge.svg)](https://github.com/PoW-HC/hashcash/actions/workflows/codeql.yml)
[![license](https://img.shields.io/github/license/PoW-HC/hashcash)](https://github.com/PoW-HC/hashcash/blob/main/LICENSE)

---

Hashcash is a Go library which implements the hashcash [proof-of-work](https://en.wikipedia.org/wiki/Proof_of_work)
algorithm. Hashcash has been used as a denial-of-service counter measure technique in a number of systems. To learn more
about hashcash visit [official page](http://hashcash.org/).

## Usage

Computing a valid hashcash:

```go
package main

import (
"context"
"fmt"

"github.com/PoW-HC/hashcash/pkg/hash"
"github.com/PoW-HC/hashcash/pkg/pow"
)

const maxIterations = 1 << 30

func main() {
hasher, err := hash.NewHasher("sha256")
if err != nil {
// handle error
}

p := pow.New(hasher)

hashcash, err := pow.InitHashcash(5, "127.0.0.1", pow.SignExt("secret", hasher))
if err != nil {
// handle error
}

solution, err := p.Compute(context.Background(), hashcash, maxIterations)
if err != nil {
// handle error
}
fmt.Println(solution)
}

```

Outputs:

```
1:5:1649257375:127.0.0.1:41965c8500f67f79b35672d7fb7e19fe5af0d51da582cbfcdbedb0f5944198bb:MgjmhCBiRV4=:MTk3YzA0
```

```shell
echo -n "1:5:1649257375:127.0.0.1:41965c8500f67f79b35672d7fb7e19fe5af0d51da582cbfcdbedb0f5944198bb:MgjmhCBiRV4=:MTk3YzA0" | shasum -a 256
0000067c716fa612cee5eb31eab6163e804c002841cfe96cc81f4f8034fd6006 -
```

Verification token:

```go
err := p.Verify(solution, "127.0.0.1")
if err != nil {
// hashcash token failed verification.
}
```

## Documentation

https://pkg.go.dev/github.com/PoW-HC/hashcash

## License

See the [LICENSE](LICENSE.md) file for license rights and limitations (MIT).
21 changes: 21 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module github.com/PoW-HC/hashcash

go 1.16

require (
github.com/abice/go-enum v0.4.0
github.com/golang/mock v1.6.0
github.com/minio/sha256-simd v1.0.0
github.com/stretchr/testify v1.7.1
)

require (
github.com/google/uuid v1.3.0 // indirect
github.com/klauspost/cpuid/v2 v2.0.12 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
golang.org/x/crypto v0.0.0-20220331220935-ae2d96664a29 // indirect
golang.org/x/mod v0.6.0-dev.0.20220330205332-605edab4323b // indirect
golang.org/x/sys v0.0.0-20220405210540-1e041c57c461 // indirect
)
Loading

0 comments on commit 5c75ccf

Please sign in to comment.