-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
go code splitted, random permutation and user storing array created, …
…padding changed
- Loading branch information
Showing
25 changed files
with
606 additions
and
456 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package crypto | ||
|
||
import ( | ||
"crypto/cipher" | ||
"crypto/rand" | ||
"errors" | ||
"main/utils" | ||
|
||
"golang.org/x/crypto/chacha20poly1305" | ||
) | ||
|
||
var ( | ||
SYMM_NODE_KEY []byte | ||
SYMM_NODE_AEAD cipher.AEAD | ||
) | ||
|
||
func GenerateSymmetricalAlgorithm() (cipher.AEAD, []byte, error) { | ||
key := make([]byte, chacha20poly1305.KeySize) | ||
if _, err := rand.Read(key); err != nil { | ||
return nil, nil, utils.JoinError("symmetrical key reading error", err) | ||
} | ||
|
||
aead, err := chacha20poly1305.NewX(key) | ||
if err != nil { | ||
return nil, nil, utils.JoinError("symmetrical key creation error", err) | ||
} | ||
|
||
return aead, key, nil | ||
} | ||
|
||
func ParseSymmetricalAlgorithm(key []byte) (cipher.AEAD, error) { | ||
aead, err := chacha20poly1305.NewX(key) | ||
if err != nil { | ||
return nil, utils.JoinError("symmetrical key parsing error", err) | ||
} | ||
|
||
return aead, nil | ||
} | ||
|
||
func EncryptSymmetrical(plaintext []byte, aead cipher.AEAD) ([]byte, error) { | ||
nonce := make([]byte, aead.NonceSize(), aead.NonceSize()+len(plaintext)+aead.Overhead()) | ||
if _, err := rand.Read(nonce); err != nil { | ||
return nil, utils.JoinError("symmetrical encryption error", err) | ||
} | ||
|
||
return aead.Seal(nonce, nonce, plaintext, nil), nil | ||
} | ||
|
||
func DecryptSymmetrical(ciphertext []byte, aead cipher.AEAD) ([]byte, error) { | ||
if len(ciphertext) < aead.NonceSize() { | ||
return nil, errors.New("ciphertext too short") | ||
} | ||
|
||
nonce, ciphertext := ciphertext[:aead.NonceSize()], ciphertext[aead.NonceSize():] | ||
result, err := aead.Open(nil, nonce, ciphertext, nil) | ||
if err != nil { | ||
return nil, utils.JoinError("symmetrical decryption error", err) | ||
} | ||
|
||
return result, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
module main/m/v2 | ||
module main | ||
|
||
go 1.18 | ||
|
||
|
Oops, something went wrong.