v / vlib / x / crypto / curve25519 / examples / shared_sec_with_raw_bytes.v
23 lines · 20 sloc · 1009 bytes · d32969ed3ea071346c5d110d9c68cc09e604cc64
Raw
1// Copyright © 2025 blackshirt.
2// Use of this source code is governed by an MIT license
3// that can be found in the LICENSE file.
4//
5// This code snippet give us an example on how to use this `curve25519` module
6// on creating shared secret through `x25519` function thats accepts raw bytes
7import encoding.hex
8import x.crypto.curve25519
9
10fn main() {
11 // Sample of randomly generated 32 bytes length of the key as a Alice key
12 a_privkey := '844f744729d93369147d48d82d18b979f0b4be5f27b4b21c68fd42804575fe29'
13 mut alice_privkey := hex.decode(a_privkey)!
14
15 // For example, Alice receives Bob's public key
16 b_pubkey := '7f869b403ddb1f5708bc2f8246ae78fa53ea304af585d5bb14fea3aafe5315c7'
17 bob_pubkey := hex.decode(b_pubkey)!
18
19 // Then, Alice can generated shared secret to be shared with Bob
20 shared_sec := curve25519.x25519(mut alice_privkey, bob_pubkey)!
21 dump(shared_sec.hex()) // shared_sec.hex(): dfa1c80d488e3de14389a852ae8f4b2f6831f8e5cea80694c7ea104ffe694858
22 dump(shared_sec.len) // shared_sec.len: 32
23}
24