Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
xtaci authored Jul 15, 2024
1 parent 667074e commit c9e4c9e
Showing 1 changed file with 68 additions and 56 deletions.
124 changes: 68 additions & 56 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ To use HPPK, you need to have Go installed. You can download and install Go from
To generate a new pair of private and public keys:

```go
package main
import (
"fmt"
"github.com/xtaci/hppk"
Expand All @@ -67,45 +69,35 @@ func main() {
}
```
### Encrypting a Message
### Encryption
To encrypt a message using the public key:
```go
package main
import (
"crypto/rand"
"fmt"
"math/big"
"github.com/xtaci/hppk"
)
func main() {
// Generate a key pair
pk, sk, err := hppk.GenerateKey(rand.Reader, 512)
privKey, err := hppk.GenerateKey(10)
if err != nil {
fmt.Println("Error generating keys:", err)
return
panic(err)
}
pubKey := privKey.Public()
// Message to encrypt
message := []byte("Hello, World!")
// Encrypt the message
P, Q, err := hppk.Encrypt(pk, message)
message := []byte("hello world")
kem, err := hppk.Encrypt(pubKey, message)
if err != nil {
fmt.Println("Error encrypting message:", err)
return
panic(err)
}
fmt.Println("Encrypted P:", P)
fmt.Println("Encrypted Q:", Q)
fmt.Printf("Encrypted KEM: %+v\n", kem)
}
```
### Decrypting a Message
### Decryption
To decrypt the encrypted values using the private key:
Expand All @@ -115,63 +107,83 @@ package main
import (
"fmt"
"github.com/xtaci/hppk"
"math/big"
)
func main() {
// Assuming pk, sk, P, and Q are already defined as in the encryption example
privKey, err := hppk.GenerateKey(10)
if err != nil {
panic(err)
}
pubKey := privKey.Public()
// Decrypt the message
decryptedMessage, err := sk.Decrypt(P, Q)
message := []byte("hello world")
kem, err := hppk.Encrypt(pubKey, message)
if err != nil {
fmt.Println("Error decrypting message:", err)
return
panic(err)
}
fmt.Println("Decrypted Message:", string(decryptedMessage.Bytes()))
decryptedMessage, err := privKey.Decrypt(kem)
if err != nil {
panic(err)
}
fmt.Printf("Decrypted Message: %s\n", decryptedMessage)
}
```
### Signing
```go
package main
import (
"crypto/sha256"
"fmt"
"github.com/xtaci/hppk"
)
func main() {
privKey, err := hppk.GenerateKey(10)
if err != nil {
panic(err)
}
digest := sha256.Sum256([]byte("hello world"))
signature, err := privKey.Sign(digest[:])
if err != nil {
panic(err)
}
fmt.Printf("Signature: %+v\n", signature)
}
```
### Signing and VerifySignature
### Verification
```go
package main
import (
"crypto/rand"
"fmt"
"math/big"
"github.com/xtaci/hppk"
"crypto/sha256"
"fmt"
"github.com/xtaci/hppk"
)
func main() {
// Generate a new private key for signing
order := 10 // Example order, should be >= 5
privateKey, err := hppk.GenerateKey(order)
if err != nil {
fmt.Println("Error generating private key:", err)
return
}
// Define a message to sign
message := []byte("Hello, world!")
// Sign the message using the private key
signature, err := privateKey.Sign(message)
if err != nil {
fmt.Println("Error signing message:", err)
return
}
// Verify the signature using the corresponding public key
isValid := hppk.VerifySignature(signature, message, &privateKey.PublicKey)
if isValid {
fmt.Println("Signature is valid.")
} else {
fmt.Println("Signature is not valid.")
}
privKey, err := hppk.GenerateKey(10)
if err != nil {
panic(err)
}
pubKey := privKey.Public()
digest := sha256.Sum256([]byte("hello world"))
signature, err := privKey.Sign(digest[:])
if err != nil {
panic(err)
}
isValid := hppk.VerifySignature(signature, digest[:], pubKey)
fmt.Printf("Signature valid: %v\n", isValid)
}
```
## Contributing
Expand Down

0 comments on commit c9e4c9e

Please sign in to comment.