Skip to content

Commit

Permalink
v1.0.1 (#2)
Browse files Browse the repository at this point in the history
* chore(path): Change path import
* docs: Change docs
  • Loading branch information
putuadityabayu committed Aug 27, 2023
1 parent 5b095f3 commit b5db520
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/crypto_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
crypto-test:
strategy:
matrix:
go-version: [1.19.x]
go-version: [1.19.x,1.20.x,1.21.x]
platform: [ubuntu-latest, windows-latest, macos-latest]

runs-on: ${{ matrix.platform }}
Expand Down
21 changes: 21 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
go.work
.env
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"go.testEnvFile": "D:\\Coding\\portalnesia\\go-crypto\\.env"
}
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
# Go-Crypto
[![Go Reference](https://pkg.go.dev/badge/go.portalnesia.com/crypto.svg)](https://pkg.go.dev/go.portalnesia.com/crypto) ![Unit Testing](https://github.com/portalnesia/go-crypto/actions/workflows/crypto_test.yml/badge.svg)

# Crypto

Crypto package for Internal Portalnesia

## Install

```bash
go get github.com/portalnesia/go-crypto
go get go.portalnesia.com/crypto
```

## Example
## Usage

```go
package main

import (
crypto "github.com/portalnesia/go-crypto"
"go.portalnesia.com/crypto"
"fmt"
)

func main() {
c := crypto.PortalnesiaCrypto("YOUR_SECRET_KEY")
c := crypto.New("YOUR_SECRET_KEY")

text := "Hello World"

Expand All @@ -34,4 +36,4 @@ func main() {
```

## Go References
[pkg.go.dev/github.com/portalnesia/go-crypto](https://pkg.go.dev/github.com/portalnesia/go-crypto)
[pkg.go.dev/go.portalnesia.com/crypto](https://pkg.go.dev/go.portalnesia.com/crypto)
6 changes: 5 additions & 1 deletion crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type CryptoKey struct {
key []byte
}

func PortalnesiaCrypto(secret string) CryptoKey {
func New(secret string) CryptoKey {
key := []byte(secret)
return CryptoKey{key: key}
}
Expand Down Expand Up @@ -55,6 +55,10 @@ func (crypto CryptoKey) Encrypt(data string) (string, error) {
}

func (crypto CryptoKey) Decrypt(encrypted string) (string, error) {
if len(encrypted) <= 0 {
return "", nil
}

split := strings.Split(encrypted, ":")

encrypted = split[0] + split[1]
Expand Down
9 changes: 2 additions & 7 deletions crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,9 @@ func GetConfig() (string, string) {

func TestCrypto(t *testing.T) {
TEXT, KEY := GetConfig()
fmt.Printf("Text: %s\n", TEXT)

crypto := PortalnesiaCrypto(KEY)

crypto := New(KEY)
encrypted, err := crypto.Encrypt(TEXT)

fmt.Printf("Encrypted: %s\n", encrypted)

if err != nil {
t.Error(err)
}
Expand All @@ -46,7 +41,7 @@ func TestFromNodeJs(t *testing.T) {

const encryptedFromNodeJs = "af215d824448b4b21d05a90768ae289a:01d3612196d88e088ffe22695078fe0cfb27af565c438d7c2680c14fd84b78645d28eef1c32f4e5e723f9e3e9e3a6811"

crypto := PortalnesiaCrypto(KEY)
crypto := New(KEY)

decrypted, err := crypto.Decrypt(encryptedFromNodeJs)

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/portalnesia/go-crypto
module go.portalnesia.com/crypto

go 1.19
go 1.21

require (
github.com/joho/godotenv v1.4.0
Expand Down
15 changes: 7 additions & 8 deletions password_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,25 @@ import (
"fmt"
"os"
"testing"

"github.com/joho/godotenv"
)

var (
PASS = "passwordfromtesting"
HASHED string
pass = "passwordfromtesting"
hashed = HashPassword(pass)
)

func TestHashPassword(t *testing.T) {
HASHED = HashPassword(PASS)
fmt.Println(HASHED)
}

func TestCompareHashedPassword(t *testing.T) {
ok := ComparePassword(PASS, HASHED)
ok := ComparePassword(pass, hashed)
if !ok {
t.Errorf("Password not match")
}
}

func TestNodeJsComparePassword(t *testing.T) {
godotenv.Load(".env")
fmt.Println("TES", os.Getenv("NODEJS_PASS"), os.Getenv("NODEJS_HASHED"))
ok := ComparePassword(os.Getenv("NODEJS_PASS"), os.Getenv("NODEJS_HASHED"))
if !ok {
t.Errorf("Password not match")
Expand Down

0 comments on commit b5db520

Please sign in to comment.