mirror of
https://github.com/status-im/status-go.git
synced 2026-09-01 09:31:12 +00:00
Part of the Go project layout migration, item 27. Pure move plus import-path rewrite across 502 files. No API or behaviour change. `internal/` keeps the messaging application logic unimportable from outside the module, which is what the issue asks for -- status-go is consumed through the C-bindings in mobile/, not as a Go library. Things that had to follow the move, beyond the Go imports: - tools/generate-handlers/template.txt. messenger_handlers.go is generated, and the template hard-codes the imports it emits, so the generated file kept importing protocol/common and failed typecheck. - .gitignore. The ignore rule for that generated file was pinned to the old path; without moving it, a 1486-line generated file starts being tracked. - Makefile: the logosstorage and torrent test targets (both the archive packages and ./protocol itself), the archive README, migration-protocol. - scripts/run_unit_tests.sh, which names the protocol package explicitly to shard its tests. - scripts/cleanup_generated_files.sh and .golangci.yml. scripts/migration_check.sh also needed a fix that is not specific to this move: it validated every file the branch touched under a migration dir against the timestamp naming rule, and a directory rename makes every migration in it look newly added. It now excludes renames, so moving a migration is not mistaken for adding one. refs #7067
111 lines
2.3 KiB
Go
111 lines
2.3 KiB
Go
package common
|
|
|
|
import (
|
|
"crypto/aes"
|
|
"crypto/cipher"
|
|
"crypto/ecdsa"
|
|
"crypto/rand"
|
|
"errors"
|
|
"io"
|
|
"math/big"
|
|
|
|
"golang.org/x/crypto/sha3"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto/ecies"
|
|
|
|
"github.com/status-im/status-go/internal/crypto"
|
|
)
|
|
|
|
const (
|
|
nonceLength = 12
|
|
defaultECHDSharedKeyLength = 16
|
|
defaultECHDMACLength = 16
|
|
)
|
|
|
|
var (
|
|
ErrInvalidCiphertextLength = errors.New("invalid cyphertext length")
|
|
|
|
letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
|
numberRunes = []rune("0123456789")
|
|
alphanumericRunes = append(numberRunes, letterRunes...)
|
|
)
|
|
|
|
func HashPublicKey(pk *ecdsa.PublicKey) []byte {
|
|
return Shake256(crypto.CompressPubkey(pk))
|
|
}
|
|
|
|
func Decrypt(cyphertext []byte, key []byte) ([]byte, error) {
|
|
if len(cyphertext) < nonceLength {
|
|
return nil, ErrInvalidCiphertextLength
|
|
}
|
|
|
|
c, err := aes.NewCipher(key)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
gcm, err := cipher.NewGCM(c)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
nonce := cyphertext[:nonceLength]
|
|
return gcm.Open(nil, nonce, cyphertext[nonceLength:], nil)
|
|
}
|
|
|
|
func Encrypt(plaintext []byte, key []byte, reader io.Reader) ([]byte, error) {
|
|
c, err := aes.NewCipher(key)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
gcm, err := cipher.NewGCM(c)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
nonce := make([]byte, gcm.NonceSize())
|
|
if _, err = io.ReadFull(reader, nonce); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return gcm.Seal(nonce, nonce, plaintext, nil), nil
|
|
}
|
|
|
|
func Shake256(buf []byte) []byte {
|
|
h := make([]byte, 64)
|
|
sha3.ShakeSum256(h, buf)
|
|
return h
|
|
}
|
|
|
|
func MakeECDHSharedKey(yourPrivateKey *ecdsa.PrivateKey, theirPubKey *ecdsa.PublicKey) ([]byte, error) {
|
|
return ecies.ImportECDSA(yourPrivateKey).GenerateShared(
|
|
ecies.ImportECDSAPublic(theirPubKey),
|
|
defaultECHDSharedKeyLength,
|
|
defaultECHDMACLength,
|
|
)
|
|
}
|
|
|
|
func randomString(choice []rune, n int) (string, error) {
|
|
max := big.NewInt(int64(len(choice)))
|
|
rr := rand.Reader
|
|
|
|
b := make([]rune, n)
|
|
for i := range b {
|
|
pos, err := rand.Int(rr, max)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
b[i] = choice[pos.Int64()]
|
|
}
|
|
return string(b), nil
|
|
}
|
|
|
|
func RandomAlphabeticalString(n int) (string, error) {
|
|
return randomString(letterRunes, n)
|
|
}
|
|
|
|
func RandomAlphanumericString(n int) (string, error) {
|
|
return randomString(alphanumericRunes, n)
|
|
}
|