-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
84 lines (71 loc) · 2.51 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"crypto/sha256"
"encoding/base32"
"encoding/hex"
"fmt"
"time"
"github.com/libsv/go-bk/bec"
"github.com/libsv/go-bk/wif"
"github.com/pquerna/otp/totp"
)
func main() {
now := time.Now()
randomHash := sha256.Sum256([]byte("alice@wallet.com connection request to bob@wallet.com at " + now.String()))
fmt.Println("Alice generates a random hash and shares it with bob along with her corresponding publicKey")
fmt.Println("randomHash: ", hex.EncodeToString(randomHash[:]))
// here's what alice knows
aliceWif := "L31jqxoa4e8hU2zXDNZVgzuNJfYqgchcxdkDfzc2TL1sAKTy2tSa"
alice, _ := wif.DecodeWIF(aliceWif)
a := alice.PrivKey.ToECDSA()
// see can calculate
Ax, Ay := bec.S256().ScalarMult(a.X, a.Y, randomHash[:])
// she shares A (point coordinates)
Apub := bec.PublicKey{
X: Ax,
Y: Ay,
Curve: bec.S256(),
}
fmt.Println("alice public key: ", hex.EncodeToString(Apub.SerialiseCompressed()), "\n")
// here's what bob knows
bobWif := "KzCktW7nsKWehHVTgwsaYgpy4RHq9YcGUtW2ezDMwtgjWjpjJAYy"
bob, _ := wif.DecodeWIF(bobWif)
b := bob.PrivKey.ToECDSA()
// he can calculate
Bx, By := bec.S256().ScalarMult(b.X, b.Y, randomHash[:])
// he shares B point coordinates
Bpub := bec.PublicKey{
X: Bx,
Y: By,
Curve: bec.S256(),
}
fmt.Println("Bob is able to calculate his corresponding public key and shares that.")
fmt.Println("bob public key: ", hex.EncodeToString(Bpub.SerialiseCompressed()), "\n")
// alice can now calculate
aSx, aSy := bec.S256().ScalarMult(Bx, By, a.D.Bytes())
aliceSecret := bec.PublicKey{
X: aSx,
Y: aSy,
Curve: bec.S256(),
}
// bob can now calculate
bSx, bSy := bec.S256().ScalarMult(Ax, Ay, b.D.Bytes())
bobSecret := bec.PublicKey{
X: bSx,
Y: bSy,
Curve: bec.S256(),
}
// they should be the same
fmt.Println("They each calculate a shared secret using their private key and the counterpart's derived public key.")
fmt.Println("alice secret: ", hex.EncodeToString(aliceSecret.X.Bytes()))
fmt.Println("bob secret: ", hex.EncodeToString(bobSecret.X.Bytes()), "\n")
sharedSecret := base32.StdEncoding.EncodeToString(aliceSecret.X.Bytes())
fmt.Println("Alice and Bob can now use the shared secret to generate a time based set of one time passwords (TOTP) to authenticate each other.")
for x := 0; x < 10; x++ {
now := time.Now()
aliceOTP, _ := totp.GenerateCode(sharedSecret, now)
bobOTP, _ := totp.GenerateCode(sharedSecret, now)
fmt.Printf("aliceOTP: %s, bobOTP: %s\n", aliceOTP, bobOTP)
time.Sleep(30 * time.Second)
}
}