Browse Source

Cloned from Dr. Maiti

master
Khoi Trinh 2 years ago
commit
1cca38f3de
  1. 2
      README.md
  2. 28
      hashing-examples.go
  3. 15
      hello.go
  4. 127
      rsa-encrypt-sign.go

2
README.md

@ -0,0 +1,2 @@
# CS5970S22

28
hashing-examples.go

@ -0,0 +1,28 @@
package main
import (
"crypto/md5"
"crypto/sha256"
"fmt"
)
func main() {
s := "6xAg8YNc5j8ihgKcr9tGjc2Mfdlpmpfnke9NyI7i3L27GAgeVB5trbxvyKpjPkepfzNoH7Z2bTyZ0UwgYBzEGb3j4XVK0GoNit6kgkR4ZZDkXU26fG8NPJ7BcFfYffp3vOTGvVJohZgrldBXdum6ryaTfVPq2GP1i9RxkgX71XkDo0KRfb8zdBTAwCDvHPcgpIBMzqdWnQ3ycEXZKuYgp6OchYVilFSNUgcrQpavKPx8aV8gilx5vOqX1MHJKlPjDZ7bZd7B6mE22ITne9y5ttj1PFPKoQksWQOeDn1teKoKME2aTBdaCpq0pd25JiYsMepAe3zWjpIcZtkLVcRxYpGzCgGlFqCFT1zZusIwauQcLRrOlH80lgZScxcQPv10pCqbHfWpy2nDBbntYUxrEHjpSzAnpsb6BLTh33g2ZNmO1FV08ihZZDw8yh6FzYLGFjWAOeD9ub11lRk0ERGmIILb7D0pOE5znWyqwSAghGSK11DFi0qA4ajdZJam291FOqYuwrImUpGlzPIfWDzoUAnhMFNCXrsHSWxJBV2nvEyfFwRTxBGYjhkvwZJa8Mg8H38P6Cz8bTJhUXhi0YHN5kpnY0nmy1NwaKXq6qGxdfFIJAmISGtn3qiYStkLmXXy0UGfeSCcPy7O3ohUvRLB4sFkyVigzH8KZrZ0wOvY6hSLnZPnUBB1Asc37Y8BbquyG0JQ87xehmDwsxOblrjxNiLyztJK8Pz2Z3A5T6I0sHmO7P6xgohKyExEnFEeW6I0lXQg6a4q7DTuMZ0vVmc5SWPJ7HmfNG5ecB53I3IgA2D38wWZNUoXpz4MizTOrciftndi2gQuHQImWrazm7KsYTGWf9WpZvoSDZl3CM2K8zisdghnPFy1B23tROAmPiupYDiplnYER76383i2RuTxRpMBmFVxNF9bqFxIQFjPYCpUorKrl1zlufM4vXGFY3CmyASmcyYL46kUPcjKfY6A9WlCPxRx7Wo8vdg4OvIe"
sha256 := sha256.Sum256([]byte(s))
md5 := md5.Sum([]byte(s))
fmt.Println()
fmt.Println(s)
fmt.Println()
fmt.Printf("%x", sha256)
fmt.Println()
fmt.Println()
fmt.Printf("%x", md5)
fmt.Println()
fmt.Println()
}

15
hello.go

@ -0,0 +1,15 @@
package main
import "fmt"
func main() {
fmt.Println("hello world")
sum := 0
for i := 0; i < 10; i++ {
fmt.Println(sum)
sum += i
}
fmt.Println(sum)
}

127
rsa-encrypt-sign.go

@ -0,0 +1,127 @@
// Derived from: https://medium.com/@bobgzm/golang-cryptography-rsa-asymmetric-algorithm-e91363a2f7b3
package main
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"fmt"
"os"
)
func main() {
alicePrivateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
alicePublicKey := &alicePrivateKey.PublicKey
bobPrivateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
bobPublicKey := &bobPrivateKey.PublicKey
fmt.Println("Alice's Private Key : ", alicePrivateKey)
fmt.Println()
fmt.Println("Alice's Public key ", alicePublicKey)
fmt.Println()
fmt.Println("Bob's Private Key : ", bobPrivateKey)
fmt.Println()
fmt.Println("Bob's Public key ", bobPublicKey)
fmt.Println()
message := []byte("first rule of cs5970 is that you tell everyone about cs5970")
label := []byte("")
hash := sha256.New()
ciphertext, err := rsa.EncryptOAEP(
hash,
rand.Reader,
bobPublicKey,
message,
label)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// OAEP is a padding scheme, used with RSA
fmt.Printf("OAEP encrypted [%s] to \n[%x]\n", string(message), ciphertext)
fmt.Println()
ciphertext2, err := rsa.EncryptOAEP(
hash,
rand.Reader,
bobPublicKey,
message,
label)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// OAEP is a padding scheme, used with RSA
fmt.Printf("OAEP encrypted [%s] to \n[%x]\n", string(message), ciphertext2)
fmt.Println()
var opts rsa.PSSOptions
opts.SaltLength = rsa.PSSSaltLengthAuto // for simple example
PSSmessage := message
newhash := crypto.SHA256
pssh := newhash.New()
pssh.Write(PSSmessage)
hashed := pssh.Sum(nil)
signature, err := rsa.SignPSS(
rand.Reader,
alicePrivateKey,
newhash,
hashed,
&opts)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Probabilistic Signature Scheme (PSS)
fmt.Printf("PSS Signature : %x\n", signature)
fmt.Println()
plainText, err := rsa.DecryptOAEP(
hash,
rand.Reader,
bobPrivateKey,
ciphertext,
label)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("OAEP decrypted [%x] to \n[%s]\n", ciphertext, plainText)
fmt.Println()
err = rsa.VerifyPSS(
alicePublicKey,
newhash,
hashed,
signature,
&opts)
if err != nil {
fmt.Println("Signature verification failed!")
os.Exit(1)
} else {
fmt.Println("Verify Signature successful!")
}
}
Loading…
Cancel
Save