Skip to content

Commit

Permalink
crypt
Browse files Browse the repository at this point in the history
  • Loading branch information
zRich committed Oct 27, 2022
1 parent 48291e7 commit 52a93d3
Show file tree
Hide file tree
Showing 8 changed files with 336 additions and 1 deletion.
80 changes: 80 additions & 0 deletions crypto/crypto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// The AGPLv3 License (AGPLv3)

// Copyright (c) 2022 ZHAO Zhenhua <[email protected]>

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.

// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package crypto

import (
gocrypto "crypto"
"hash"
)

type Key interface {
//the key's raw byte
Bytes() ([]byte, error)
//PrivateKey returns true is this is a asymmetric private key or symmetric security key
PrivateKey() bool
//symmetric returns true if this key is symmetric, otherwise false
Symmetric() bool
//if this is a asymmetric key, returns the corresponding Public key, otherwise error
PublicKey() (Key, error)
}

// Key generation options for BHPCSP
type KeyGenOpts interface {
Algorithem() string
}

// HashOpts contains hash options for BHPCSP
type HashOpts interface {
Algorithem() string
}

// EncrypterOpts contains encrypting options
type EncrypterOpts interface {
}

// DecrypterOpts contains decrypting options
type DecrypterOpts interface {
}

// SignerOpts contain signing options
type SignerOpts interface {
gocrypto.SignerOpts
}

// bytehub+ crytograhic service provider
type BHPCSP interface {
//KeyGen generates a new key
KeyGen(opts KeyGenOpts) (Key, error)

//GetKey returns the key
GetKey(keyInstance []byte) (Key, error)

//Hash hashes a message
Hash(msg []byte, opts HashOpts) ([]byte, error)

//GetHash returns the instance of hash function
GetHash(opt HashOpts) (hash.Hash, error)

Encrypt(k Key, plaintext []byte, opts EncrypterOpts) ([]byte, error)
Decrypt(k Key, ciphertext []byte, opts DecrypterOpts) ([]byte, error)

//Sign signs a message's hash
Sign(k Key, digest []byte, opts SignerOpts) ([]byte, error)
//Verify verifies a signature
Verify(k Key, signature, digest []byte, opts SignerOpts) (bool, error)
}
122 changes: 122 additions & 0 deletions crypto/ed29919csp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// The AGPLv3 License (AGPLv3)

// Copyright (c) 2022 ZHAO Zhenhua <[email protected]>

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.

// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package crypto

import (
"crypto/ed25519"
"crypto/rand"
"errors"
"hash"
)

type Ed25519PrivateKey struct {
csp []byte
pub Ed25519PublicKey
}

// KeyGen generates a new key
func (e *Ed25519PrivateKey) KeyGen(opts KeyGenOpts) (Key, error) {
return e.generateEd25519Key()
}

func (k *Ed25519PrivateKey) generateEd25519Key() (Key, error) {
pub, ski, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
return nil, errors.New("failed to genenate ed25519 key")
}
key := &Ed25519PrivateKey{ski, Ed25519PublicKey{ski, &pub}}
return key, nil
}

// GetKey returns the key
func (e *Ed25519PrivateKey) GetKey(keyInstance []byte) (Key, error) {
panic("not implemented") // TODO: Implement
}

// Hash hashes a message
func (e *Ed25519PrivateKey) Hash(msg []byte, opts HashOpts) ([]byte, error) {
panic("not implemented") // TODO: Implement
}

// GetHash returns the instance of hash function
func (e *Ed25519PrivateKey) GetHash(opt HashOpts) (hash.Hash, error) {
panic("not implemented") // TODO: Implement
}

func (e *Ed25519PrivateKey) Encrypt(k Key, plaintext []byte, opts EncrypterOpts) ([]byte, error) {
panic("not implemented") // TODO: Implement
}

func (e *Ed25519PrivateKey) Decrypt(k Key, ciphertext []byte, opts DecrypterOpts) ([]byte, error) {
panic("not implemented") // TODO: Implement
}

// Sign signs a message's hash
func (e *Ed25519PrivateKey) Sign(k Key, digest []byte, opts SignerOpts) ([]byte, error) {
panic("not implemented") // TODO: Implement
}

// Verify verifies a signature
func (e *Ed25519PrivateKey) Verify(k Key, signature []byte, digest []byte, opts SignerOpts) (bool, error) {
panic("not implemented") // TODO: Implement
}

// the key's raw byte
func (e *Ed25519PrivateKey) Bytes() ([]byte, error) {
return e.csp, nil
}

// PrivateKey returns true is this is a asymmetric private key or symmetric security key
func (e *Ed25519PrivateKey) PrivateKey() bool {
return true
}

// symmetric returns true if this key is symmetric, otherwise false
func (e *Ed25519PrivateKey) Symmetric() bool {
return false
}

// if this is a asymmetric key, returns the corresponding Public key, otherwise false
func (e *Ed25519PrivateKey) PublicKey() (Key, error) {
return &e.pub, nil
}

type Ed25519PublicKey struct {
csi []byte
pub *ed25519.PublicKey
}

// the key's raw byte
func (e *Ed25519PublicKey) Bytes() ([]byte, error) {
return e.csi, nil
}

// PrivateKey returns true is this is a asymmetric private key or symmetric security key
func (e *Ed25519PublicKey) PrivateKey() bool {
return false
}

// symmetric returns true if this key is symmetric, otherwise false
func (e *Ed25519PublicKey) Symmetric() bool {
return false
}

// if this is a asymmetric key, returns the corresponding Public key, otherwise false
func (e *Ed25519PublicKey) PublicKey() (Key, error) {
return e, nil
}
1 change: 1 addition & 0 deletions crypto/ed29919csp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package crypto
24 changes: 24 additions & 0 deletions crypto/opts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// The AGPLv3 License (AGPLv3)

// Copyright (c) 2022 ZHAO Zhenhua <[email protected]>

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.

// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package crypto

const (
ED25519 = "ED25519"
SHA2 = "SHA2"
SHA3 = "SHA3"
)
72 changes: 72 additions & 0 deletions crypto/rsacsp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package crypto

import "hash"

type RSAPrivateKey struct {
csi []byte
// pub RSAPublicKey

}

// KeyGen generates a new key
func (r *RSAPrivateKey) KeyGen(opts KeyGenOpts) (Key, error) {
panic("not implemented") // TODO: Implement
}

// GetKey returns the key
func (r *RSAPrivateKey) GetKey(keyInstance []byte) (Key, error) {
panic("not implemented") // TODO: Implement
}

// Hash hashes a message
func (r *RSAPrivateKey) Hash(msg []byte, opts HashOpts) ([]byte, error) {
panic("not implemented") // TODO: Implement
}

// GetHash returns the instance of hash function
func (r *RSAPrivateKey) GetHash(opt HashOpts) (hash.Hash, error) {
panic("not implemented") // TODO: Implement
}

func (r *RSAPrivateKey) Encrypt(k Key, plaintext []byte, opts EncrypterOpts) ([]byte, error) {
panic("not implemented") // TODO: Implement
}

func (r *RSAPrivateKey) Decrypt(k Key, ciphertext []byte, opts DecrypterOpts) ([]byte, error) {
panic("not implemented") // TODO: Implement
}

// Sign signs a message's hash
func (r *RSAPrivateKey) Sign(k Key, digest []byte, opts SignerOpts) ([]byte, error) {
panic("not implemented") // TODO: Implement
}

// Verify verifies a signature
func (r *RSAPrivateKey) Verify(k Key, signature []byte, digest []byte, opts SignerOpts) (bool, error) {
panic("not implemented") // TODO: Implement
}

// the key's raw byte
func (r *RSAPrivateKey) Bytes() ([]byte, error) {
panic("not implemented") // TODO: Implement
}

// PrivateKey returns true is this is a asymmetric private key or symmetric security key
func (r *RSAPrivateKey) PrivateKey() bool {
panic("not implemented") // TODO: Implement
}

// symmetric returns true if this key is symmetric, otherwise false
func (r *RSAPrivateKey) Symmetric() bool {
panic("not implemented") // TODO: Implement
}

// if this is a asymmetric key, returns the corresponding Public key, otherwise error
func (r *RSAPrivateKey) PublicKey() (Key, error) {
panic("not implemented") // TODO: Implement
}

// type RSAPublicKey struct {
// csi []byte
// pub xrsa.XRsa.
// }
24 changes: 24 additions & 0 deletions node/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// The AGPLv3 License (AGPLv3)

// Copyright (c) 2022 ZHAO Zhenhua <[email protected]>

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.

// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

// this file define/implement node configuration

package node

type NodeConfig struct {
SignKey []byte
}
12 changes: 12 additions & 0 deletions node/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
nodes:
node1: 192.168.0.2:3000
node2: 192.168.0.3:3000
node3: 192.168.0.4:3000
node4: 192.168.0.5:3000
node5: 192.168.0.6:3000
node6: 192.168.0.7:3000
vaultIndex:
path: ./data
vaultPath: /data/vault/
TLS:
tls: tls.key
2 changes: 1 addition & 1 deletion node/node.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package node

import (
"crypto"
Expand Down

0 comments on commit 52a93d3

Please sign in to comment.