Skip to content

Commit 3f22711

Browse files
committed
feat: first version
0 parents  commit 3f22711

File tree

11 files changed

+414
-0
lines changed

11 files changed

+414
-0
lines changed

.editorconfig

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = LF
6+
insert_final_newline = true
7+
trim_trailing_whitespace = true
8+
indent_style = space
9+
indent_size = 4
10+
11+
[Makefile]
12+
indent_style = tab
13+
14+
[*.md]
15+
trim_trailing_whitespace = false
16+
17+
[*.go]
18+
indent_style = tab
19+
20+
[*.yml]
21+
indent_size = 2
22+
23+
[*.yaml]
24+
indent_size = 2
25+
26+
[*.yml.dist]
27+
indent_size = 2

.github/workflows/go.yml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
name: Go
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
branches: [ master ]
8+
9+
jobs:
10+
11+
build:
12+
name: Build
13+
runs-on: ubuntu-latest
14+
steps:
15+
16+
- name: Set up Go 1.x
17+
uses: actions/setup-go@v2
18+
with:
19+
go-version: ^1.16
20+
id: go
21+
22+
- name: Check out code into the Go module directory
23+
uses: actions/checkout@v2
24+
25+
- name: Build
26+
run: go build -race -v ./...
27+
28+
- name: Test
29+
run: |
30+
go test -race -cover -coverprofile ./coverage.out.tmp ./...
31+
cat ./coverage.out.tmp | grep -v '.pb.go' | grep -v 'mock_' > ./coverage.out
32+
rm ./coverage.out.tmp
33+
34+
- name: Run golangci-lint
35+
uses: golangci/golangci-lint-action@v2
36+
with:
37+
version: v1.41.1
38+
39+
- name: Coveralls
40+
uses: shogo82148/actions-goveralls@v1
41+
with:
42+
path-to-profile: coverage.out
43+
44+
finish:
45+
needs: build
46+
runs-on: ubuntu-latest
47+
steps:
48+
- name: Coveralls Finished
49+
uses: coverallsapp/github-action@master
50+
with:
51+
github-token: ${{ secrets.GITHUB_TOKEN }}
52+
parallel-finished: true

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.DS_Store
2+
*.out
3+
4+
vendor/
5+
6+
*.swp

.golangci.yml

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
run:
2+
concurrency: 4
3+
deadline: 1m
4+
issues-exit-code: 1
5+
tests: false
6+
skip-files:
7+
- ".*_mock\\.go"
8+
- "mock_.*\\.go"
9+
- ".*/pkg/mod/.*$"
10+
11+
output:
12+
format: colored-line-number
13+
print-issued-lines: true
14+
print-linter-name: true
15+
16+
linters-settings:
17+
errcheck:
18+
check-type-assertions: false
19+
check-blank: false
20+
govet:
21+
check-shadowing: false
22+
revive:
23+
ignore-generated-header: true
24+
severity: warning
25+
gofmt:
26+
simplify: true
27+
gocyclo:
28+
min-complexity: 18
29+
maligned:
30+
suggest-new: true
31+
dupl:
32+
threshold: 50
33+
goconst:
34+
min-len: 3
35+
min-occurrences: 2
36+
depguard:
37+
list-type: blacklist
38+
include-go-root: false
39+
packages:
40+
- github.com/davecgh/go-spew/spew
41+
misspell:
42+
locale: US
43+
ignore-words:
44+
- cancelled
45+
goimports:
46+
local-prefixes: go.opentelemetry.io
47+
48+
49+
linters:
50+
disable-all: true
51+
enable:
52+
- deadcode
53+
- depguard
54+
- errcheck
55+
- gas
56+
- goconst
57+
- gocyclo
58+
- gofmt
59+
- revive
60+
- govet
61+
- ineffassign
62+
- megacheck
63+
- misspell
64+
- structcheck
65+
- typecheck
66+
- unconvert
67+
- varcheck
68+
- gosimple
69+
- staticcheck
70+
- unused
71+
- asciicheck
72+
- bodyclose
73+
- dogsled
74+
- dupl
75+
- durationcheck
76+
- errorlint
77+
- exhaustive
78+
- exportloopref
79+
- forbidigo
80+
- forcetypeassert
81+
- gocritic
82+
- godot
83+
- goerr113
84+
- gosec
85+
- ifshort
86+
- nestif
87+
- nilerr
88+
- nlreturn
89+
- noctx
90+
- prealloc
91+
- predeclared
92+
- sqlclosecheck
93+
- tagliatelle
94+
- whitespace
95+
- wrapcheck
96+
- wsl
97+
fast: false
98+
99+
#issues:
100+
# exclude:
101+
# hack for go-service, fixed in v1.1.0
102+
#- "Errors unhandled."
103+
#- "Error return value of `container.Set` is not checked"
104+
#- "composite literal uses unkeyed fields" # govet
105+
# exclude-use-default: true
106+
# max-per-linter: 0
107+
# max-same: 0
108+
# new: false
109+
# new-from-rev: ""
110+
# new-from-patch: ""

LICENSE.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2021 Axel Etcheverry
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

Makefile

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
.PHONY: all clean test cover travis lint
2+
3+
release:
4+
@echo "Release v$(version)"
5+
@git pull
6+
@git checkout master
7+
@git pull
8+
@git checkout develop
9+
@git flow release start $(version)
10+
@git flow release finish $(version) -p -m "Release v$(version)"
11+
@git checkout develop
12+
@echo "Release v$(version) finished."
13+
14+
all: test
15+
16+
clean:
17+
@go clean -i ./...
18+
19+
coverage.out: $(shell find . -type f -print | grep -v vendor | grep "\.go")
20+
@go test -race -cover -coverprofile ./coverage.out.tmp ./...
21+
@cat ./coverage.out.tmp | grep -v '.pb.go' | grep -v 'mock_' > ./coverage.out
22+
@rm ./coverage.out.tmp
23+
24+
test: coverage.out
25+
26+
.PHONY: lint
27+
lint:
28+
ifeq (, $(shell which golangci-lint))
29+
@echo "Install golangci-lint..."
30+
@curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b ${GOPATH}/bin v1.41.1
31+
endif
32+
@echo "lint..."
33+
@golangci-lint run --timeout=300s ./...
34+
35+
cover: coverage.out
36+
@echo ""
37+
@go tool cover -func ./coverage.out
38+

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
Hyperscale secure [![Last release](https://img.shields.io/github/release/hyperscale-stack/secure.svg)](https://github.com/hyperscale-stack/secure/releases/latest) [![Documentation](https://godoc.org/github.com/hyperscale-stack/secure?status.svg)](https://godoc.org/github.com/hyperscale-stack/secure)
2+
====================
3+
4+
[![Go Report Card](https://goreportcard.com/badge/github.com/hyperscale-stack/secure)](https://goreportcard.com/report/github.com/hyperscale-stack/secure)
5+
6+
| Branch | Status | Coverage |
7+
|---------|--------|----------|
8+
| master | [![Build Status](https://github.com/hyperscale-stack/secure/workflows/Go/badge.svg?branch=master)](https://github.com/hyperscale-stack/secure/actions?query=workflow%3AGo) | [![Coveralls](https://img.shields.io/coveralls/hyperscale-stack/secure/master.svg)](https://coveralls.io/github/hyperscale-stack/secure?branch=master) |
9+
10+
The Hyperscale secure is a secure string generator.
11+
12+
## Example
13+
14+
```go
15+
package main
16+
17+
import (
18+
"fmt"
19+
"github.com/hyperscale-stack/secure"
20+
)
21+
22+
func main() {
23+
s, err := secure.GenerateRandomString(64)
24+
if err != nil {
25+
panic(err)
26+
}
27+
28+
fmt.Println(s)
29+
}
30+
31+
```
32+
33+
## License
34+
35+
Hyperscale secure is licensed under [the MIT license](LICENSE.md).

generate.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2021 Hyperscale. All rights reserved.
2+
// Use of this source code is governed by a MIT
3+
// license that can be found in the LICENSE file.
4+
5+
package secure
6+
7+
import (
8+
"crypto/rand"
9+
"fmt"
10+
)
11+
12+
// GenerateRandomBytes returns securely generated random bytes.
13+
func GenerateRandomBytes(n int) ([]byte, error) {
14+
b := make([]byte, n)
15+
16+
if _, err := rand.Read(b); err != nil {
17+
return nil, fmt.Errorf("read rand failed: %w", err)
18+
}
19+
20+
return b, nil
21+
}
22+
23+
// GenerateRandomString returns a securely generated random string.
24+
func GenerateRandomString(length int) (string, error) {
25+
result := make([]byte, length)
26+
27+
if _, err := rand.Read(result); err != nil {
28+
return "", fmt.Errorf("read rand failed: %w", err)
29+
}
30+
31+
for i := 0; i < length; i++ {
32+
result[i] &= 0x7F
33+
34+
// 48 : 0
35+
// 57 : 9
36+
// 65 : A
37+
// 90 : Z
38+
// 97 : a
39+
// 122 : z
40+
// [0-9A-Za-z]
41+
for (result[i] < 48 || result[i] > 57) && (result[i] < 65 || result[i] > 90) && (result[i] < 97 || result[i] > 122) {
42+
if _, err := rand.Read(result[i : i+1]); err != nil {
43+
return "", fmt.Errorf("read rand failed: %w", err)
44+
}
45+
46+
result[i] &= 0x7F
47+
}
48+
}
49+
50+
return string(result), nil
51+
}

0 commit comments

Comments
 (0)