Skip to content

Commit

Permalink
docs(crypto): change docs comment (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
putuadityabayu committed Jul 29, 2024
1 parent 2a2f009 commit c4c4107
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 170 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@

# Go workspace file
go.work
.env
.env
.idea/*
!.idea/copyright
15 changes: 0 additions & 15 deletions .idea/git_toolbox_prj.xml

This file was deleted.

9 changes: 0 additions & 9 deletions .idea/go-crypto.iml

This file was deleted.

8 changes: 0 additions & 8 deletions .idea/modules.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

119 changes: 0 additions & 119 deletions .idea/workspace.xml

This file was deleted.

24 changes: 17 additions & 7 deletions crypto.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/*
* Copyright (c) Portalnesia - All Rights Reserved
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential
* Written by Putu Aditya <aditya@portalnesia.com>
*/

package crypto

import (
Expand All @@ -12,16 +19,18 @@ import (
"github.com/mergermarket/go-pkcs7"
)

type CryptoKey struct {
type Crypto struct {
key []byte
}

func New(secret string) CryptoKey {
// New initialize Crypto instance
func New(secret string) Crypto {
key := []byte(secret)
return CryptoKey{key: key}
return Crypto{key: key}
}

func (crypto CryptoKey) Encrypt(data string) (string, error) {
// Encrypt encrypt data
func (c Crypto) Encrypt(data string) (string, error) {
plainText := []byte(data)
plainText, err := pkcs7.Pad(plainText, aes.BlockSize)

Expand All @@ -34,7 +43,7 @@ func (crypto CryptoKey) Encrypt(data string) (string, error) {
return "", err
}

block, err := aes.NewCipher(crypto.key)
block, err := aes.NewCipher(c.key)
if err != nil {
return "", err
}
Expand All @@ -54,7 +63,8 @@ func (crypto CryptoKey) Encrypt(data string) (string, error) {
return encrypted, nil
}

func (crypto CryptoKey) Decrypt(encrypted string) (string, error) {
// Decrypt decrypt data
func (c Crypto) Decrypt(encrypted string) (string, error) {
if len(encrypted) <= 0 {
return "", nil
}
Expand All @@ -68,7 +78,7 @@ func (crypto CryptoKey) Decrypt(encrypted string) (string, error) {
return "", err
}

block, err := aes.NewCipher(crypto.key)
block, err := aes.NewCipher(c.key)
if err != nil {
return "", err
}
Expand Down
7 changes: 7 additions & 0 deletions crypto_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/*
* Copyright (c) Portalnesia - All Rights Reserved
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential
* Written by Putu Aditya <aditya@portalnesia.com>
*/

package crypto

import (
Expand Down
16 changes: 11 additions & 5 deletions password.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
/*
* Copyright (c) Portalnesia - All Rights Reserved
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential
* Written by Putu Aditya <aditya@portalnesia.com>
*/

package crypto

import (
"golang.org/x/crypto/bcrypt"
)

// HashPassword hash password with bcrypt
func HashPassword(p string) string {
password := []byte(p)

Expand All @@ -14,10 +22,8 @@ func HashPassword(p string) string {
return string(hashed)
}

func ComparePassword(p string, s string) bool {
password := []byte(p)
hashed := []byte(s)

err := bcrypt.CompareHashAndPassword(hashed, password)
// ComparePassword compare password with saved hash password
func ComparePassword(password string, hasedPassword string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hasedPassword), []byte(password))
return err == nil
}
7 changes: 7 additions & 0 deletions password_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/*
* Copyright (c) Portalnesia - All Rights Reserved
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential
* Written by Putu Aditya <aditya@portalnesia.com>
*/

package crypto

import (
Expand Down

0 comments on commit c4c4107

Please sign in to comment.