Commit b05e9cf1 authored by Your Name's avatar Your Name

add generate key and init script

parent 5613ecd4
FROM cog-hello-world:latest
RUN apt-get update && apt-get install procps -y
COPY proxy proxy
# COPY my_second_process my_second_process
COPY script.sh script.sh
RUN chmod +x script.sh
CMD ./script.sh
\ No newline at end of file
CMD ./script.sh
FROM cog-hello-world:latest
RUN apt-get update && apt-get install procps -y
COPY proxy proxy
# COPY my_second_process my_second_process
COPY script.sh script.sh
RUN chmod +x script.sh
CMD ./script.sh
\ No newline at end of file
.PHONY: default metaring all clean fmt docker
ciperPrivateKey ?= 8c4c3af66c696dfe72cdedacf16c02332dfea1de7edd09f2264f5bbdf9122f430ddc9a324d79da4a855e82785a4b675fc3baf0e35f7f46763d3a14233fa072d1
# BUILD_FLAGS = -tags netgo -ldflags "-X proxyWithSign/main.Ciphertext=${INVALIDBALANCE}"
VERSION := $(shell echo $(shell git describe --tags))
# dockerdep:
# docker build -f Dockerfile.dep -t dep .
build: go.sum
@echo "ciperPrivateKey IS ${ciperPrivateKey}"
go build -ldflags '-X main.CipherPrivateKey=${ciperPrivateKey}' -o ./proxy
docker:
@echo "ciperPrivateKey IS ${ciperPrivateKey}"
go build -ldflags '-X main.CipherPrivateKey=${ciperPrivateKey}' -o ./proxy
docker build -f Dockerfile -t aiwithproxy:${VERSION} .
# 1. 基础镜像 2. 加密密钥 3. 端口 4. 输出镜像
```
go run keys/main.go
./init.sh cog-hello-world:latest privatekey 234234 output:v0.0.10
```
\ No newline at end of file
#!/bin/bash
time=$(date "+%Y-%m-%d_%H:%M:%S")
defaultTag=":latest"
inputImage=${1:-cog-hello-world${defaultTag}}
ciperPrivateKey=${2:-8c4c3af66c696dfe72cdedacf16c02332dfea1de7edd09f2264f5bbdf9122f430ddc9a324d79da4a855e82785a4b675fc3baf0e35f7f46763d3a14233fa072d1}
port=${3:-5000}
outputImage=${4:-outputImage_${time}}
# 1. 基础镜像 2. 加密密钥 3. 端口 4. 输出镜像
echo "inputImage : $inputImage"
echo "ciperPrivateKey: $ciperPrivateKey"
echo "port : $port"
echo "outputImage : $outputImage"
dockerfile="Dockerfile"
echo "FROM $inputImage" > $dockerfile
echo "RUN apt-get update && apt-get install procps -y" >> $dockerfile
echo "COPY proxy proxy" >> $dockerfile
echo "COPY script.sh script.sh" >> $dockerfile
echo "RUN chmod +x script.sh" >> $dockerfile
echo "CMD ./script.sh" >> $dockerfile
go build -ldflags '-X main.CipherPrivateKey=${ciperPrivateKey}' -o ./proxy
# version=$(git describe --tags)
# docker build -f Dockerfile -t aiwithproxy:${version} .
docker build -f Dockerfile -t $outputImage .
module keys
go 1.22.1
require github.com/miguelmota/go-ethereum-hdwallet v0.1.2
require (
github.com/FactomProject/basen v0.0.0-20150613233007-fe3947df716e // indirect
github.com/FactomProject/btcutilecc v0.0.0-20130527213604-d3a63a5752ec // indirect
github.com/btcsuite/btcd v0.22.1 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.2.0 // indirect
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 // indirect
github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
github.com/ethereum/go-ethereum v1.10.17 // indirect
github.com/tyler-smith/go-bip32 v1.0.0 // indirect
github.com/tyler-smith/go-bip39 v1.1.0 // indirect
golang.org/x/crypto v0.0.0-20220518034528-6f7dac969898 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
)
This diff is collapsed.
package main
import (
"crypto/aes"
"crypto/cipher"
"crypto/ecdsa"
"fmt"
"log"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
)
func main() {
// generate a random private key
privateKey, err := crypto.GenerateKey()
if err != nil {
log.Fatal(err)
}
// private key convert to bytes
privateKeyBytes := crypto.FromECDSA(privateKey)
// fmt.Println(hexutil.Encode(privateKeyBytes)[2:]) // d593ae0363f9e66fe42a91fe0260067439e0760a3f4f554696cd9350c41ae7a6
// private key to public key
publicKey := privateKey.Public()
// public key to hex
// slice the 0x and the first 2 character 04 which is always the EC prefix and not required
// interface typecast
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
log.Fatal("cannot assert type: publicKey is not of type *ecdsa.PublicKey")
}
publicKeyBytes := crypto.FromECDSAPub(publicKeyECDSA)
// fmt.Println(hexutil.Encode(publicKeyBytes)[4:]) // b00316b00f432d90c1a3b5ee0f0fc4416c5aab07677edcc0ca65c53eb100f92fbde998d466bf7676cb4eaa2bf78f1a1df086718d54170e47b6d59315002d61df
// generate public address from public key
address := crypto.PubkeyToAddress(*publicKeyECDSA).Hex()
// fmt.Println(address) // 0x2Fc75Ce9cE3529758b5787ADE183566F765De0b9
// to hex
// TODO (?)
// hash := sha3.NewLegacyKeccak256()
// hash.Write(publicKeyBytes[1:])
// fmt.Println(hexutil.Encode(hash.Sum(nil)[12:])) // 0x2fc75ce9ce3529758b5787ade183566f765de0b9
encrypt(string(hexutil.Encode(privateKeyBytes)[2:]))
fmt.Println("public", hexutil.Encode(publicKeyBytes)[4:])
fmt.Println("address", address)
}
func encrypt(msg string) {
key := []byte("wubanaistrong2024051132bitlength") // 32 bytes key for AES-256
//plaintext := []byte("Hello, WorldHello, WorldHello, WorldHello, WorldHello, WorldHello, WorldHello, World!")
plaintext := []byte(msg)
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
ciphertext := make([]byte, len(plaintext))
stream := cipher.NewCTR(block, make([]byte, block.BlockSize()))
stream.XORKeyStream(ciphertext, plaintext)
//fmt.Printf("Encrypted private key: %s\n", base64.StdEncoding.EncodeToString(ciphertext))
fmt.Printf("Encrypted private key: %x\n", ciphertext)
decrypted := make([]byte, len(ciphertext))
stream = cipher.NewCTR(block, make([]byte, block.BlockSize()))
stream.XORKeyStream(decrypted, ciphertext)
fmt.Printf("Decrypted: %s\n", decrypted)
}
// package main
// import (
// "crypto/rand"
// "crypto/rsa"
// "crypto/sha256"
// "crypto/x509"
// "encoding/base64"
// "encoding/pem"
// "fmt"
// "golang.org/x/crypto/ssh"
// )
// func marshalRSAPrivate(priv *rsa.PrivateKey) string {
// return string(pem.EncodeToMemory(&pem.Block{
// Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv),
// }))
// }
// func generateKey() (string, string, error) {
// reader := rand.Reader
// bitSize := 2048
// key, err := rsa.GenerateKey(reader, bitSize)
// if err != nil {
// return "", "", err
// }
// pub, err := ssh.NewPublicKey(key.Public())
// if err != nil {
// return "", "", err
// }
// pubKeyStr := string(ssh.MarshalAuthorizedKey(pub))
// privKeyStr := marshalRSAPrivate(key)
// return pubKeyStr, privKeyStr, nil
// }
// func encrypt(msg, publicKey string) (string, error) {
// parsed, _, _, _, err := ssh.ParseAuthorizedKey([]byte(publicKey))
// if err != nil {
// return "", err
// }
// // To get back to an *rsa.PublicKey, we need to first upgrade to the
// // ssh.CryptoPublicKey interface
// parsedCryptoKey := parsed.(ssh.CryptoPublicKey)
// // Then, we can call CryptoPublicKey() to get the actual crypto.PublicKey
// pubCrypto := parsedCryptoKey.CryptoPublicKey()
// // Finally, we can convert back to an *rsa.PublicKey
// pub := pubCrypto.(*rsa.PublicKey)
// encryptedBytes, err := rsa.EncryptOAEP(
// sha256.New(),
// rand.Reader,
// pub,
// []byte(msg),
// nil)
// if err != nil {
// return "", err
// }
// return base64.StdEncoding.EncodeToString(encryptedBytes), nil
// }
// func decrypt(data, priv string) (string, error) {
// data2, err := base64.StdEncoding.DecodeString(data)
// if err != nil {
// return "", err
// }
// block, _ := pem.Decode([]byte(priv))
// key, err := x509.ParsePKCS1PrivateKey(block.Bytes)
// if err != nil {
// return "", err
// }
// decrypted, err := rsa.DecryptOAEP(sha256.New(), rand.Reader, key, data2, nil)
// if err != nil {
// return "", err
// }
// return string(decrypted), nil
// }
// func main() {
// pubKey, privKey, _ := generateKey()
// fmt.Println("my public key is...")
// fmt.Println(pubKey)
// fmt.Println("my private key is...")
// fmt.Println(privKey)
// encryptedData, _ := encrypt("hello world", pubKey)
// fmt.Println("my encrypted message is...")
// fmt.Println(encryptedData)
// fmt.Println("")
// decryptedData, _ := decrypt(encryptedData, privKey)
// fmt.Println("and the decrypted message is...")
// fmt.Println(decryptedData)
// }
// package main
// import (
// "crypto/aes"
// "crypto/cipher"
// "encoding/base64"
// "fmt"
// "github.com/tyler-smith/go-bip32"
// "github.com/tyler-smith/go-bip39"
// )
// func main() {
// // Generate a mnemonic for memorization or user-friendly seeds
// entropy, _ := bip39.NewEntropy(256)
// mnemonic, _ := bip39.NewMnemonic(entropy)
// // Generate a Bip32 HD wallet for the mnemonic and a user supplied password
// seed := bip39.NewSeed(mnemonic, "Secret Passphrase")
// masterKey, _ := bip32.NewMasterKey(seed)
// publicKey := masterKey.PublicKey()
// // Display mnemonic and keys
// fmt.Println("Mnemonic: ", mnemonic)
// fmt.Println("Master private key: ", masterKey)
// fmt.Println("Master public key: ", publicKey)
// }
// package main
// import (
// "crypto/aes"
// "crypto/cipher"
// "encoding/base64"
// "fmt"
// "log"
// hdwallet "github.com/miguelmota/go-ethereum-hdwallet"
// )
// func main() {
// mnemonic := "tag volcano eight thank tide danger coast health above argue embrace heavy"
// wallet, err := hdwallet.NewFromMnemonic(mnemonic)
// if err != nil {
// log.Fatal(err)
// }
// path := hdwallet.MustParseDerivationPath("m/44'/60'/0'/0/0")
// account, err := wallet.Derive(path, false)
// if err != nil {
// log.Fatal(err)
// }
// fmt.Println(account.Address.Hex()) // 0xC49926C4124cEe1cbA0Ea94Ea31a6c12318df947
// path = hdwallet.MustParseDerivationPath("m/44'/60'/0'/0/1")
// account, err = wallet.Derive(path, false)
// if err != nil {
// log.Fatal(err)
// }
// fmt.Println(account.Address.Hex()) // 0x8230645aC28A4EdD1b0B53E7Cd8019744E9dD559
// }
# https://dev.to/elioenaiferrari/asymmetric-cryptography-with-golang-2ffd
package main
import (
"crypto/aes"
"crypto/cipher"
"encoding/hex"
"flag"
"fmt"
"sync"
......@@ -18,8 +21,14 @@ import (
"github.com/ethereum/go-ethereum/accounts"
)
var key = []byte("wubanaistrong2024051132bitlength") // 32 bytes key for AES-256
var CipherPrivateKey = ""
func main() {
fmt.Println("CipherPrivateKey", CipherPrivateKey)
var listenPort int
flag.IntVar(&listenPort, "listenPort", 6000, "proxy listen the port")
var upstreamPort int
......@@ -37,6 +46,23 @@ func main() {
// DocExpansion: "none",
// }))
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
ciphertext, err := hex.DecodeString(CipherPrivateKey)
// ciphertext, err := base64.StdEncoding.DecodeString(CipherPrivateKey)
if err != nil {
panic(err)
}
decrypted := make([]byte, len([]byte(ciphertext)))
stream := cipher.NewCTR(block, make([]byte, block.BlockSize()))
stream.XORKeyStream(decrypted, []byte(ciphertext))
// fmt.Printf("%x", decrypted)
app.All("/*", func(c *fiber.Ctx) error {
path := c.Path()
......@@ -55,7 +81,11 @@ func main() {
fmt.Println("c.Response().Body():", string(c.Response().Body()))
local, _ := crypto.HexToECDSA("4df7e0d1c1329d03c72ed0cec631adc048ae85178d3528e7f55368da0901ed5b")
// fmt.Printf("Decrypted: %s\n", decrypted)
//local, _ := crypto.HexToECDSA("4df7e0d1c1329d03c72ed0cec631adc048ae85178d3528e7f55368da0901ed5b")
local, _ := crypto.HexToECDSA(string(decrypted))
sig, err := crypto.Sign(h[:], local)
if err != nil {
......
No preview for this file type
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment