-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.js
110 lines (94 loc) · 3.9 KB
/
App.js
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import React, { Component } from 'react'
import { StyleSheet, Text, View } from 'react-native'
import nacl from 'tweet-nacl-react-native-expo'
export default class App extends Component {
exampleKeyEncodeDecode = async () => {
// simple example encode decode (with base64)
// usefull for sharing keys with others or storing in file
const keyPair = await nacl.box.keyPair()
const { publicKey, secretKey } = keyPair
const base64EncodedPublic = nacl.util.encodeBase64(publicKey)
const base64EncodedPrivate = nacl.util.encodeBase64(secretKey)
const base64DecodedPublic = nacl.util.decodeBase64(base64EncodedPublic)
const base64DecodedPrivate = nacl.util.decodeBase64(base64EncodedPrivate)
console.log({
publicKey,
secretKey,
base64EncodedPublic,
base64EncodedPrivate,
base64DecodedPublic,
base64DecodedPrivate
})
}
exampleEncryptDecrypt = async () => {
// simple encrypt decrypt example
// uses self secretKey together with others publicKey to derive a sharedKey (Diffie–Hellman logic)
// sharedKey is used to encrypt and decrypt the messages between the two (symmetric encryption)
const bobKeyPair = await nacl.box.keyPair()
const aliceKeyPair = await nacl.box.keyPair()
// Alice and Bob will derive the same shared key
const bobSharedKey = nacl.box.before(aliceKeyPair.publicKey, bobKeyPair.secretKey)
const aliceSharedKey = nacl.box.before(bobKeyPair.publicKey, aliceKeyPair.secretKey)
// Bob decodes the message he wants to send
// (for messages we will use UTF8 encoding/decoding)
// Bob generates a random nonce value
// Bob encrypts the decoded message using the nonce and the sharedKey
// Bob encodes the encrypted message (Uint8Array) into a string (base64)
// Note that Bob will send the encrypted message together
// with nonce (plain text) to Alice in order for her to decrypt
const str = "Hello Alice , this is Bob! ;'[]{} bla bla"
const strDecoded = new Uint8Array(nacl.util.decodeUTF8(str))
const nonce = await nacl.randomBytes(24)
const bobEncryptedStr = nacl.box.after(strDecoded, nonce, bobSharedKey)
const bobBase64EncryptedStr = nacl.util.encodeBase64(bobEncryptedStr)
// Alice decodes the string (base64) to Uint8Array (this gives her bobEncryptedStr)
// Alice decrypts the message using the nonce and the sharedKey (this gives her strDecoded)
// Alice then encodes the result to get the final message in plain text
const messageFromBobDecoded = nacl.util.decodeBase64(bobBase64EncryptedStr) // same as bobEncryptedStr
const messageFromBobDecrypted = nacl.box.open.after(messageFromBobDecoded, nonce, aliceSharedKey) // same as strDecoded
const messageFromBobEncoded = nacl.util.encodeUTF8(messageFromBobDecrypted) // same as str
console.log({
bob: {
keypair: {
publicKey: nacl.util.encodeBase64(bobKeyPair.publicKey),
secretKey: nacl.util.encodeBase64(bobKeyPair.secretKey)
},
sharedKey: nacl.util.encodeBase64(bobSharedKey),
messagePlain: str,
messageEncrypted: bobBase64EncryptedStr
},
alice: {
keypair: {
publicKey: nacl.util.encodeBase64(aliceKeyPair.publicKey),
secretKey: nacl.util.encodeBase64(aliceKeyPair.secretKey)
},
sharedKey: nacl.util.encodeBase64(aliceSharedKey),
messageDecrypted: messageFromBobEncoded,
},
})
}
componentDidMount = async () => {
// await this.exampleKeyEncodeDecode()
await this.exampleEncryptDecrypt()
}
render() {
return (
<View style={styles.container}>
<Text style={styles.text}>Hello World!</Text>
</View>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'blue',
alignItems: 'center',
justifyContent: 'center',
},
text: {
color: "white",
fontSize: 30,
fontWeight: "bold"
}
})