Skip to content

Commit

Permalink
feat: first version
Browse files Browse the repository at this point in the history
  • Loading branch information
euskadi31 committed Jul 22, 2021
0 parents commit 3f22711
Show file tree
Hide file tree
Showing 11 changed files with 414 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
root = true

[*]
charset = utf-8
end_of_line = LF
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = space
indent_size = 4

[Makefile]
indent_style = tab

[*.md]
trim_trailing_whitespace = false

[*.go]
indent_style = tab

[*.yml]
indent_size = 2

[*.yaml]
indent_size = 2

[*.yml.dist]
indent_size = 2
52 changes: 52 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: Go

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:

build:
name: Build
runs-on: ubuntu-latest
steps:

- name: Set up Go 1.x
uses: actions/setup-go@v2
with:
go-version: ^1.16
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v2

- name: Build
run: go build -race -v ./...

- name: Test
run: |
go test -race -cover -coverprofile ./coverage.out.tmp ./...
cat ./coverage.out.tmp | grep -v '.pb.go' | grep -v 'mock_' > ./coverage.out
rm ./coverage.out.tmp
- name: Run golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.41.1

- name: Coveralls
uses: shogo82148/actions-goveralls@v1
with:
path-to-profile: coverage.out

finish:
needs: build
runs-on: ubuntu-latest
steps:
- name: Coveralls Finished
uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
parallel-finished: true
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.DS_Store
*.out

vendor/

*.swp
110 changes: 110 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
run:
concurrency: 4
deadline: 1m
issues-exit-code: 1
tests: false
skip-files:
- ".*_mock\\.go"
- "mock_.*\\.go"
- ".*/pkg/mod/.*$"

output:
format: colored-line-number
print-issued-lines: true
print-linter-name: true

linters-settings:
errcheck:
check-type-assertions: false
check-blank: false
govet:
check-shadowing: false
revive:
ignore-generated-header: true
severity: warning
gofmt:
simplify: true
gocyclo:
min-complexity: 18
maligned:
suggest-new: true
dupl:
threshold: 50
goconst:
min-len: 3
min-occurrences: 2
depguard:
list-type: blacklist
include-go-root: false
packages:
- github.com/davecgh/go-spew/spew
misspell:
locale: US
ignore-words:
- cancelled
goimports:
local-prefixes: go.opentelemetry.io


linters:
disable-all: true
enable:
- deadcode
- depguard
- errcheck
- gas
- goconst
- gocyclo
- gofmt
- revive
- govet
- ineffassign
- megacheck
- misspell
- structcheck
- typecheck
- unconvert
- varcheck
- gosimple
- staticcheck
- unused
- asciicheck
- bodyclose
- dogsled
- dupl
- durationcheck
- errorlint
- exhaustive
- exportloopref
- forbidigo
- forcetypeassert
- gocritic
- godot
- goerr113
- gosec
- ifshort
- nestif
- nilerr
- nlreturn
- noctx
- prealloc
- predeclared
- sqlclosecheck
- tagliatelle
- whitespace
- wrapcheck
- wsl
fast: false

#issues:
# exclude:
# hack for go-service, fixed in v1.1.0
#- "Errors unhandled."
#- "Error return value of `container.Set` is not checked"
#- "composite literal uses unkeyed fields" # govet
# exclude-use-default: true
# max-per-linter: 0
# max-same: 0
# new: false
# new-from-rev: ""
# new-from-patch: ""
19 changes: 19 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2021 Axel Etcheverry

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.
38 changes: 38 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
.PHONY: all clean test cover travis lint

release:
@echo "Release v$(version)"
@git pull
@git checkout master
@git pull
@git checkout develop
@git flow release start $(version)
@git flow release finish $(version) -p -m "Release v$(version)"
@git checkout develop
@echo "Release v$(version) finished."

all: test

clean:
@go clean -i ./...

coverage.out: $(shell find . -type f -print | grep -v vendor | grep "\.go")
@go test -race -cover -coverprofile ./coverage.out.tmp ./...
@cat ./coverage.out.tmp | grep -v '.pb.go' | grep -v 'mock_' > ./coverage.out
@rm ./coverage.out.tmp

test: coverage.out

.PHONY: lint
lint:
ifeq (, $(shell which golangci-lint))
@echo "Install golangci-lint..."
@curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b ${GOPATH}/bin v1.41.1
endif
@echo "lint..."
@golangci-lint run --timeout=300s ./...

cover: coverage.out
@echo ""
@go tool cover -func ./coverage.out

35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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)
====================

[![Go Report Card](https://goreportcard.com/badge/github.com/hyperscale-stack/secure)](https://goreportcard.com/report/github.com/hyperscale-stack/secure)

| Branch | Status | Coverage |
|---------|--------|----------|
| 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) |

The Hyperscale secure is a secure string generator.

## Example

```go
package main

import (
"fmt"
"github.com/hyperscale-stack/secure"
)

func main() {
s, err := secure.GenerateRandomString(64)
if err != nil {
panic(err)
}

fmt.Println(s)
}

```

## License

Hyperscale secure is licensed under [the MIT license](LICENSE.md).
51 changes: 51 additions & 0 deletions generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2021 Hyperscale. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.

package secure

import (
"crypto/rand"
"fmt"
)

// GenerateRandomBytes returns securely generated random bytes.
func GenerateRandomBytes(n int) ([]byte, error) {
b := make([]byte, n)

if _, err := rand.Read(b); err != nil {
return nil, fmt.Errorf("read rand failed: %w", err)
}

return b, nil
}

// GenerateRandomString returns a securely generated random string.
func GenerateRandomString(length int) (string, error) {
result := make([]byte, length)

if _, err := rand.Read(result); err != nil {
return "", fmt.Errorf("read rand failed: %w", err)
}

for i := 0; i < length; i++ {
result[i] &= 0x7F

// 48 : 0
// 57 : 9
// 65 : A
// 90 : Z
// 97 : a
// 122 : z
// [0-9A-Za-z]
for (result[i] < 48 || result[i] > 57) && (result[i] < 65 || result[i] > 90) && (result[i] < 97 || result[i] > 122) {
if _, err := rand.Read(result[i : i+1]); err != nil {
return "", fmt.Errorf("read rand failed: %w", err)
}

result[i] &= 0x7F
}
}

return string(result), nil
}
Loading

0 comments on commit 3f22711

Please sign in to comment.