Browse Source

En cours de développement

master
losyme 9 months ago
parent
commit
7e782e72f6
  1. 15
      .editorconfig
  2. 22
      LICENSE
  3. 10
      README.md
  4. 22
      Taskfile.yml
  5. 124
      crypto.go
  6. 10
      go.mod
  7. 6
      go.sum

15
.editorconfig

@ -0,0 +1,15 @@
root = true
[*]
indent_style = space
indent_size = 4
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[*.go]
indent_style = tab
[*.md]
trim_trailing_whitespace = false

22
LICENSE

@ -1,9 +1,19 @@
MIT License
MIT License Copyright (c) 2021 losyme
Copyright (c) <year> <copyright holders>
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:
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 (including the next
paragraph) shall be included in all copies or substantial portions of the
Software.
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.
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.

10
README.md

@ -1,3 +1,11 @@
# crypto
AFAIRE
A FAIRE.
## Licence
MIT.
---
Copyright (c) 2021 `losyme`.

22
Taskfile.yml

@ -0,0 +1,22 @@
## https://taskfile.dev
##---------------------
version: '3'
tasks:
lint:
cmds:
- golangci-lint run
test:
cmds:
- go test -cover -v ./...
default:
cmds:
- task: lint
- task: test
git:
cmds:
- git add .
- git commit -m "En cours de développement"
- git push

124
crypto.go

@ -0,0 +1,124 @@
/*
------------------------------------------------------------------------------------------------------------------------
####### crypto ####### Copyright (c) 2021 losyme ################################################### MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package crypto
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"io"
"forge.chapril.org/losyme/errors"
)
type Crypto struct {
key []byte
}
func New() *Crypto {
return &Crypto{
key: []byte{
0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF,
0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF,
},
}
}
func (c *Crypto) SetKey(key string) error {
hasher := sha256.New()
if _, err := hasher.Write([]byte(key)); err != nil {
return err
}
c.key = hasher.Sum(nil)
return nil
}
func (c *Crypto) Encrypt(data []byte) ([]byte, error) {
block, err := aes.NewCipher(c.key)
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
nonce := make([]byte, gcm.NonceSize())
if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
return nil, err
}
return gcm.Seal(nonce, nonce, data, nil), nil
}
func (c *Crypto) EncryptString(text string) (string, error) {
data, err := c.Encrypt([]byte(text))
if err != nil {
return "", errors.WithMessage(err, "impossible to encrypt the string") /////////////////////////////////////////
}
return base64.StdEncoding.EncodeToString(data), nil
}
func (c *Crypto) Decrypt(data []byte) ([]byte, error) {
block, err := aes.NewCipher(c.key)
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
nonceSize := gcm.NonceSize()
if nonceSize > len(data) {
return nil, errors.New("not enough data") //////////////////////////////////////////////////////////////////////
}
nonce, cipherData := data[:nonceSize], data[nonceSize:]
plainData, err := gcm.Open(nil, nonce, cipherData, nil)
if err != nil {
return nil, err
}
return plainData, nil
}
func (c *Crypto) DecryptString(text string) (string, error) {
decoded, err := base64.StdEncoding.DecodeString(text)
if err != nil {
return "", errors.WithMessage( /////////////////////////////////////////////////////////////////////////////////
err,
"impossible to decode this string",
"string", text,
)
}
data, err := c.Decrypt(decoded)
if err != nil {
return "", errors.WithMessage( /////////////////////////////////////////////////////////////////////////////////
err,
"impossible to decrypt this string",
"string", text,
)
}
return string(data), nil
}
/*
######################################################################################################## @(°_°)@ #######
*/

10
go.mod

@ -0,0 +1,10 @@
module forge.chapril.org/losyme/crypto
go 1.17
require forge.chapril.org/losyme/errors v0.0.0-20210913194042-2345b2123826
require (
forge.chapril.org/losyme/buffer v0.0.0-20210913191354-01205c1431fe // indirect
forge.chapril.org/losyme/kvfmt v0.0.0-20210913192208-cb9f3f6469b0 // indirect
)

6
go.sum

@ -0,0 +1,6 @@
forge.chapril.org/losyme/buffer v0.0.0-20210913191354-01205c1431fe h1:9PU9dNEkMZ9gfiYI9WOlBswsECPK8TNLFjW+02nYbEE=
forge.chapril.org/losyme/buffer v0.0.0-20210913191354-01205c1431fe/go.mod h1:4Yl585hmF3bWZfuwGF2Ym8ErykJZiF9yawlmK7szIkc=
forge.chapril.org/losyme/errors v0.0.0-20210913194042-2345b2123826 h1:FFjfViZgkJmdW0iuVW8vn8X0YRM91pGC9oOqRYV/yYs=
forge.chapril.org/losyme/errors v0.0.0-20210913194042-2345b2123826/go.mod h1:GtH8H0bNCXgCjVINTRKwjxiBxazTPUS3MvGvNtAW7RU=
forge.chapril.org/losyme/kvfmt v0.0.0-20210913192208-cb9f3f6469b0 h1:VI6nPXsXR1gcfnBgC4FfFJIwx8hfg7Ww5380B30K1ZA=
forge.chapril.org/losyme/kvfmt v0.0.0-20210913192208-cb9f3f6469b0/go.mod h1:wx8WatI1yOr7DnEKGfzHbekKBJAnuAe1v67IrCjJDA0=
Loading…
Cancel
Save