Compare commits

...

284 Commits

Author SHA1 Message Date
b73fd9e64e feat: add ml kem demo 2025-09-24 23:15:32 +08:00
5ef3b4d3e6 feat: add __security/keyring-demo/ 2025-09-14 17:52:26 +08:00
3dda4e8e5b feat: add pingora-demo 2025-08-17 15:50:54 +08:00
a1024943ff feat: update serde json test 2025-08-03 16:48:22 +08:00
9eba818de9 feat: update serde json 2025-08-03 16:43:05 +08:00
814b5e9442 feat: add clap-test-3 2025-08-03 16:33:52 +08:00
6d7273c6a3 feat: add crypto bcrypt issue test 2025-07-12 21:25:42 +08:00
bd9ea78e53 feat: add __term/tui-markup-demo/ 2025-07-12 18:24:46 +08:00
73d58350c2 feat: udpates 2025-07-12 13:23:01 +08:00
e45d14498c feat: add __wasm/rust-wasm-plugins-examples/ 2025-07-12 13:22:38 +08:00
f2fd57c342 feat: add bc1-btc-address 2025-07-03 00:27:01 +08:00
ed54a7e6ef feat: add orion-demo 2025-06-28 23:22:26 +08:00
7c0865fafd feat: add orion-demo 2025-06-28 23:22:11 +08:00
Hatter Jiang
6e681f0ba1 feat: updates 2025-06-28 09:59:35 +08:00
Hatter Jiang
0dcbb75879 feat: updates 2025-06-28 09:58:50 +08:00
Hatter Jiang
ad25ed2453 feat: add passkey-rs 2025-06-28 09:50:50 +08:00
Hatter Jiang
0491407e58 feat: add passkey-rs 2025-06-28 09:50:38 +08:00
564977aad7 feat: add __alloc 2025-06-27 23:11:48 +08:00
c966ad621d feat: add eth-address 2025-06-25 00:34:06 +08:00
Jiang Hatter
9982409535 feat: reset 2025-06-15 20:10:32 +08:00
Jiang Hatter
9e8cbf557d feat: add tcp-connect-reset 2025-06-15 20:08:54 +08:00
ada9691adc feat: add axum demo 2025-03-16 22:10:36 +08:00
48ad5122f2 add dns-server-tutorial 2025-03-08 21:24:00 +08:00
10672a86b1 feat: rename comfy table 2025-01-06 23:55:15 +08:00
744b16af6c feat: add __args 2025-01-01 09:54:00 +08:00
2ff99a832d feat: update jose-test 2024-11-14 00:23:44 +08:00
4f048b3981 feat: update jose-test 2024-11-14 00:18:55 +08:00
49d91c0dc1 feat: update jose-test 2024-11-14 00:17:29 +08:00
9d9575843c feat: update jose-test 2024-11-14 00:14:25 +08:00
1eb7aeab42 feat: update jose-test 2024-11-14 00:04:36 +08:00
cc4c793835 feat: update jose-test 2024-11-13 23:54:06 +08:00
1b59475ab9 feat: udpate jose-test 2024-11-13 23:10:28 +08:00
0f85300a74 feat: update jose-test 2024-11-13 22:46:58 +08:00
e4f4e70951 feat: rsa jwk 2024-11-13 01:19:06 +08:00
ae8747de70 feat: rsa jwk 2024-11-13 01:14:34 +08:00
2a9d6dfaac feat: jose 2024-11-13 00:15:30 +08:00
9b2c40d662 feat: updates 2024-11-10 22:07:46 +08:00
04a58e9f66 feat: add jost-test 2024-11-10 21:58:37 +08:00
1ae6bb6737 feat: update crypto/aes_ctr_test 2024-08-31 19:39:19 +08:00
8717ca7594 feat: udpate __crypto/aes_ctr_test 2024-08-31 17:35:14 +08:00
09129b38a1 feat: udpate __crypto/aes_ctr_test 2024-08-31 17:34:13 +08:00
7f0e18bbe4 feat: add __crypto/aes_ctr_test/ 2024-08-31 17:11:16 +08:00
22344b6d96 feat: add native db demo 2024-05-04 11:15:28 +08:00
f107f82b7c feat: add concurrent/waitgroup demo 2024-05-01 23:53:06 +08:00
50fa9b5179 feat: add duckdb test 2024-04-13 23:23:22 +08:00
46bbd4859e feat: add duckdb test 2024-04-13 23:23:09 +08:00
e5a56888ad feat: add asn1-der-test 2024-03-30 21:42:04 +08:00
6b752c4a77 feat: add asn1-der-test 2024-03-30 21:33:59 +08:00
18e7545bcf feat: add tests asn1-der-test 2024-03-30 21:33:44 +08:00
37f94aad3f feat: add __crypto/liboqs-rust-demo 2024-03-24 13:30:51 +08:00
f5a41fdb02 feat: add __network/html-crawl-parse, __wasm/wasmtime-serde-demo 2024-03-24 13:15:59 +08:00
8adc5a58bc feat: update readme 2024-03-24 11:32:42 +08:00
31ba44c45e feat: add tokio-cpu-affinity 2024-03-24 11:32:07 +08:00
a3c45895e1 feat: add rust-rawsock-demo,tokio-kcp-demo 2024-03-24 10:40:50 +08:00
051eb5fecc feat: add rust-rawsock-demo 2024-03-24 10:15:37 +08:00
930c35abff feat: add pi 2024-03-24 00:30:31 +08:00
e5748b1e41 feat: add async-fn 2024-03-23 23:34:02 +08:00
bb36536347 feat: add async fn 2024-03-09 13:14:31 +08:00
0d1e08fcd1 feat: add tests 2024-01-20 21:54:12 +08:00
47830618e4 feat: add dialoguer-demo 2024-01-11 00:34:20 +08:00
fe5c16450c feat: update tests 2024-01-08 00:16:29 +08:00
97baed2e2a feat: add tests 2024-01-07 23:51:29 +08:00
7ec4c4a526 feat: add tests 2024-01-07 18:26:09 +08:00
526dd4172b feat: add der_parser_test 2023-11-25 23:40:00 +08:00
11278926bf feat: upate ed25519 verify 2023-10-14 00:30:18 +08:00
d41b499913 feat: add ed25519 verify 2023-10-14 00:29:03 +08:00
55e5b732f6 feat: age 2023-10-05 18:11:31 +08:00
05afe67de7 feat: ecdh with openpgp card x25519 2023-10-05 17:25:25 +08:00
a32ef922c7 feat: updates 2023-09-09 00:33:22 +08:00
11aaa45456 feat: update dependencies 2023-08-26 02:23:18 +08:00
e8b5167490 feat: update justfile 2023-08-26 01:43:59 +08:00
607b78b6b8 feat: add justfile 2023-08-26 01:35:28 +08:00
49c352e84c feat: update version 2023-08-26 01:01:50 +08:00
a26e3e1ffe feat: add secretshare 2023-08-26 00:05:54 +08:00
27ee89dd8a feat: update aegis-test 2023-08-19 23:54:16 +08:00
45eb023741 feat: updat eaegis test 2023-08-19 23:34:41 +08:00
a321aae4d2 feat: aegis-test 2023-08-19 17:30:51 +08:00
12a91e6103 feat: add tests 2023-08-09 01:02:37 +08:00
8904df2753 feat: update fetch-rs 2023-06-11 22:36:04 +08:00
49fefb745c feat: update fetch-rs 2023-06-11 22:21:29 +08:00
38b8fbe37f feat: udpate fetch-rs 2023-06-04 14:20:40 +08:00
8aba71a72b feat: update fetch-rs 2023-06-04 14:09:25 +08:00
c259d6c102 feat: add fetch-rs 2023-06-04 13:54:54 +08:00
2b71b456a3 feat: add cursive-demo/README.md 2023-04-15 21:20:43 +08:00
04914f33ab feat: add cursive-demo 2023-04-15 21:19:55 +08:00
4fb7c049b5 feat: add __crypto README.md 2023-04-15 14:44:50 +08:00
00bedd5ce1 feat: add network ngrok-demo 2023-04-01 20:34:13 +08:00
1defe5958f feat: add object-demo 2023-03-17 00:36:18 +08:00
e7f03687dc feat: use fake random 2023-03-12 22:05:55 +08:00
f7b23d0943 feat: update yubikey-rs-demo 2023-03-12 20:06:10 +08:00
12e24652b7 feat: outputs serial 2023-03-12 08:44:53 +08:00
591154f331 feat: output public key 2023-03-11 14:01:14 +08:00
390a142561 feat: udpate reamde 2023-03-11 11:50:47 +08:00
0f94dae70a feat: read flot meta 2023-03-11 11:50:07 +08:00
8e03973dda feat: add yubikey-rs-demo 2023-03-11 00:32:57 +08:00
9e06880a81 feat: add __network/ipnet-demo 2023-02-26 22:51:19 +08:00
9ec922f965 feat: add __compress/zstd-demo 2023-02-26 22:39:56 +08:00
651c96d658 feat: add rpgp-demo 2023-02-26 16:13:16 +08:00
88c931bc5c feat: add comment 2023-02-18 00:26:45 +08:00
c95a566c58 feat: add aes gcm crypto 2023-02-18 00:12:33 +08:00
8803a8500a feat: add crypto2-demo 2023-02-12 13:37:29 +08:00
d1b1132cc3 feat: add nix demo 2023-01-19 23:23:44 +08:00
2e256c32f8 feat: add reference rust-memalloc 2023-01-19 23:10:49 +08:00
f73e6ce7f8 feat: readme 2023-01-19 23:02:39 +08:00
ead7eddf1f feat: add malloc&free 1 2023-01-19 23:00:54 +08:00
e44aa7f60c feat: update memory 2023-01-19 22:22:47 +08:00
137e04eda0 feat: add __internal/memory 2023-01-19 22:19:54 +08:00
6e4277a53b feat: update epoch interuppt 2023-01-19 00:45:26 +08:00
c173ce8dba feat: add example hello 2023-01-19 00:13:30 +08:00
33cd9b007c feat: add example multi value 2023-01-19 00:11:43 +08:00
e6b985a695 feat: add examples gcd, memory 2023-01-19 00:01:28 +08:00
3eefa3cba1 feat: update examples 2023-01-18 23:57:29 +08:00
d514f80f64 feat: update example4 2023-01-18 23:48:15 +08:00
8862192778 feat: add example4 2023-01-18 23:46:14 +08:00
1bc45c72b1 feat: add example3 2023-01-18 23:34:02 +08:00
14d22ef4ef feat: update wasmtime version 2023-01-18 23:31:15 +08:00
3a98d7da3f feat: add humantime-demo 2023-01-17 23:32:05 +08:00
570b578596 feat: add __fuse/README.md 2023-01-08 12:03:00 +08:00
8f6805f3e3 feat: add crypt4ghfs-rust 2023-01-08 11:48:40 +08:00
c8837b21f6 feat: add sharmir demo 2023-01-01 20:54:02 +08:00
93a8832b36 feat: add daemonize demo 2023-01-01 20:40:01 +08:00
aa50d63aec feat: add a histrical wit-bindgen 2023-01-01 00:25:48 +08:00
01e8f5a959 feat: add fuel 2022-12-31 11:44:33 +08:00
ea815f1c0d feat: udpate readme 2022-12-31 10:35:55 +08:00
34c04da05d feat: update readme, add regexp test 2022-12-31 00:16:59 +08:00
a0359637e2 feat: update boa_engine 2022-12-31 00:01:47 +08:00
2a3d63d1e8 feat: add readme 2022-12-30 23:04:15 +08:00
448f7e42ac fix: compile warning 2022-12-30 22:24:44 +08:00
060c6b2263 feat: add calltoc 2022-12-30 13:04:31 +08:00
b98542fff3 feat: update lock 2022-12-29 21:02:05 +08:00
1098220058 feat: extism works 2022-12-04 15:00:28 +08:00
78561aec5b feat: extism 2022-12-04 14:40:25 +08:00
78db54db6c feat: add extism 2022-12-04 14:25:05 +08:00
4efe9d4a12 feat: update readme 2022-11-20 20:41:55 +08:00
820417832b feat: add external vcard-cli 2022-11-20 20:39:30 +08:00
8979029e29 feat: add fast-qr 2022-11-20 20:36:56 +08:00
3a4d6b2d62 feat: add efficient_sm2 2022-11-13 20:21:57 +08:00
acb1878e8d feat: update dubbo-rust readme 2022-10-29 00:40:01 +08:00
c18b025ac4 feat: update readme 2022-10-29 00:38:54 +08:00
cb12afd2da feat: add __rpc 2022-10-28 00:50:48 +08:00
6873d68129 feat: update README 2022-10-16 21:16:56 +08:00
a49d639b18 feat: add keychain-services 2022-10-16 21:16:13 +08:00
9ab7685f2d feat: add security-framework 2022-10-16 19:45:34 +08:00
e972998e09 feat: add highwayhash 2022-10-07 23:05:32 +08:00
2f4df9395d feat: add icalendar 2022-10-07 20:58:32 +08:00
2bb467898e feat: update hatter test 2022-10-07 14:07:51 +08:00
fe3f2af4e9 feat: add hatter-test/cargo lock 2022-10-07 00:36:39 +08:00
5337f7f61d feat: add hatter-test 2022-10-07 00:31:00 +08:00
20cd2d6877 feat: add __shuttle 2022-10-07 00:24:42 +08:00
50a4d71ad8 feat: add secrets 2022-10-03 18:34:27 +08:00
dc213c4055 feat: add java&rust 2022-09-26 00:23:56 +08:00
36e1609002 feat: update ssh-rs 2022-09-04 16:54:01 +08:00
338d2720ac feat: add ssh-rs 2022-09-02 23:58:14 +08:00
bce6338c2b feat: update readme 2022-08-28 11:52:08 +08:00
4c5d44d553 chore: reorg 2022-08-28 11:49:05 +08:00
0b0d4f2f9c feat: add __lang/rusty_jsc/, build_readme.rs 2022-08-28 11:40:00 +08:00
5d1372905f feat: update quinn 2022-08-25 01:35:27 +08:00
60eb54ca49 feat: works 2022-08-25 00:52:26 +08:00
8e29384cb2 feat: add ec 2022-08-01 00:02:03 +08:00
6833f7b86a feat: add __lang readme 2022-07-31 19:33:49 +08:00
acf61e1520 feat: add secmm-proc 2022-07-27 23:25:03 +08:00
3e755b465c feat: add readme 2022-07-27 00:26:38 +08:00
1eb9b95070 feat: update 2022-07-27 00:19:28 +08:00
4b32063d93 feat: add seckey 2022-07-27 00:14:34 +08:00
e4af4e6e25 feat: add sensitive 2022-07-27 00:07:33 +08:00
debc750e2f feat: container 2022-07-24 01:06:59 +08:00
842c5b4579 feat: container 2022-07-23 16:54:36 +08:00
b908736c1c feat: add container timing outputs 2022-07-23 14:44:14 +08:00
46b730e235 feat: update container 2022-07-23 13:03:43 +08:00
c40ac23cf5 feat: update container 2022-07-23 12:50:05 +08:00
1c9669a4a7 feat: update container 2022-07-23 12:48:43 +08:00
f0c3aa9df5 feat: update wit-bindgen-sample 2022-07-23 10:58:45 +08:00
4b4eacf3f4 feat: update wit-bindgen-sample 2022-07-23 10:50:20 +08:00
7d769d3a0f feat: wit-bindgen-sample 2022-07-23 10:48:31 +08:00
a116b1c2e6 feat: update container 2022-07-23 00:21:43 +08:00
0415152500 feat: update container 2022-07-23 00:21:32 +08:00
d90b1e69ea feat: update fetch 2022-07-22 23:54:20 +08:00
ca32d8701c feat: udpate container 2022-07-21 01:18:42 +08:00
b80d8b1859 feat: update readme 2022-07-17 20:10:04 +08:00
05ebf862fd feat: patch js-wasm 2022-07-17 19:41:12 +08:00
1de8f9499a fix: js-wasm 2022-07-17 19:40:16 +08:00
9f61cc748a feat: add js-wasm 2022-07-17 19:39:28 +08:00
30ac42001b feat: update to sjon 2022-07-17 12:55:15 +08:00
0741768d29 feat: reqwest 2022-07-17 12:46:24 +08:00
15bd8076b1 feat: add fetch timeout 2022-07-17 11:59:13 +08:00
cbc05226e0 feat: update us 2022-07-17 11:41:59 +08:00
4e88c90fd4 feat: update continer 2022-07-17 11:38:27 +08:00
2d6b1e3a1b feat: test script 2022-07-17 10:48:23 +08:00
6890520435 feat: fetch 2022-07-17 10:37:34 +08:00
74a202f1ed feat: works 2022-07-17 10:11:20 +08:00
4ba63b4c2e feat: add wit-bindgen-sample container 2022-07-17 00:06:54 +08:00
b0e1a89020 feat: add wit-bindgen-sample 2022-07-16 23:38:51 +08:00
dd45671b35 feat: update wasmtime 2022-07-16 16:18:16 +08:00
51685c6284 feat: update wasmtime 2022-07-16 16:03:51 +08:00
4d45c2dbcf feat: update wasmtime 2022-07-16 16:00:31 +08:00
fdee5bef03 feat: update wasmtime justfile 2022-07-10 14:57:36 +08:00
2a6b904c2a feat: wasmtime 2022-07-10 14:48:03 +08:00
6160185ace feat: add obfstr 2022-07-09 23:19:00 +08:00
a8a284163d feat: add boas wasn js engine 2022-07-09 19:59:06 +08:00
d17c718ee8 feat: udpate boa test 2022-07-08 00:58:22 +08:00
e2d6b9c54f feat: add deno_rust_wasm_import_functions 2022-07-08 00:36:47 +08:00
09c20602e9 feat: add result output 2022-07-06 00:56:20 +08:00
206470cdb3 feat: update justfile 2022-07-04 00:09:55 +08:00
89ce27b02f feat: html but not works 2022-07-04 00:08:41 +08:00
46783e8398 feat: update just file 2022-07-03 22:58:11 +08:00
e0b2b59858 feat: works better 2022-07-03 22:54:11 +08:00
4359817513 feat: add deno_rust_wasm_qr_decode 2022-07-03 00:34:15 +08:00
713e1424f9 feat: add wasmtime 2022-07-02 22:38:05 +08:00
f90941f497 feat: add wasmtime 2022-07-02 22:37:49 +08:00
22e77db82c feat: update tabled 2022-06-24 00:35:45 +08:00
af706d9abb feat: add select3 2022-06-18 23:53:19 +08:00
9ceaf9a789 feat: add fuser 2022-06-11 17:32:21 +08:00
fc23a5959b feat: add fuser 2022-06-11 17:32:07 +08:00
8ae8a57671 feat: add bonsaidb 2022-06-11 17:23:13 +08:00
d74850d236 feat: update mailsend 2022-06-06 00:18:09 +08:00
d181d970de feat: add mailsend 2022-06-06 00:05:36 +08:00
fbfe58c0e7 feat: update readme 2022-06-03 19:35:12 +08:00
cf46e8f119 feat: update monkey-rs 2022-06-02 00:39:33 +08:00
44759d1a67 feat: update monkey-rs 2022-06-02 00:39:22 +08:00
0488154e6f feat: update monkey-rs 2022-06-02 00:33:31 +08:00
2330a646b7 feat: add monkey-rs 2022-06-02 00:32:03 +08:00
6a8d8ca043 feat: add tabled 2022-05-21 21:44:38 +08:00
fe46aef388 feat: add sqlite extersion 2022-05-21 21:20:45 +08:00
7034c0b86c feat: add ureq 2022-05-14 01:29:43 +08:00
92bfe1819a feat: btc-address add current 2022-05-02 19:09:24 +08:00
6517457d0d feat: update btc-address 2022-05-02 19:01:30 +08:00
fc2a5217b3 feat: update druid 2022-05-02 10:42:42 +08:00
5538db42d9 feat: update readme 2022-04-15 23:50:38 +08:00
791c18ea77 feat: add dingo link 2022-04-15 23:49:53 +08:00
fe2f615943 feat: s2n quic test 2022-04-15 01:05:16 +08:00
72a8969453 feat: add hmac_sha1 2022-04-03 22:30:16 +08:00
46d24a8e24 feat: add gob 2022-03-26 10:47:58 +08:00
2b3e2a7c87 feat: bytes 2022-03-25 23:20:23 +08:00
f104c9dec9 feat: update bytes 2022-03-25 23:13:08 +08:00
8fa4284e6c feat: tokio stream 2022-03-22 00:43:39 +08:00
3f38ba99f7 feat: update mini tokio 2022-03-20 23:53:57 +08:00
18b7b4707a feat: add mini tokio 2022-03-20 23:46:17 +08:00
3684ae79da feat: add select2 2022-03-20 09:58:06 +08:00
779ecc08f2 feat: add select 2022-03-20 01:20:55 +08:00
0b0c1e4f11 refactor: rename handles 2022-03-19 23:59:33 +08:00
cbb8f43ad3 feat: add README.md 2022-03-19 23:45:47 +08:00
7007a77334 refactor: methods 2022-03-19 23:36:45 +08:00
effb8587b1 chore:rename 2022-03-19 23:17:40 +08:00
87f21a0b93 chore: clean code 2022-03-19 23:05:03 +08:00
e0b7a148ba feat: multiple stream by channel 2022-03-19 22:55:26 +08:00
db42a77035 feat: multiple message 2022-03-19 22:39:55 +08:00
43bd89f477 feat: add stream fork/join 2022-03-19 21:06:39 +08:00
f1fcb8d6f4 feat: update async study 2022-03-19 17:43:50 +08:00
03c3e1dab5 feat: works 2022-03-19 12:13:59 +08:00
0534c43968 feat: add async study 2022-03-19 10:47:04 +08:00
3486a5aae2 feat: add bunt 2022-02-05 18:32:58 +08:00
aca23fa6fd feat: add qcell 2022-02-03 22:02:18 +08:00
315ccf100e feat: add qcell 2022-02-03 22:01:55 +08:00
8339336888 feat: add orbtk 2021-08-15 17:45:29 +08:00
4a873ab1f6 feat: add pretty print 2021-08-08 11:14:32 +08:00
c20df9c739 feat: sign data 2021-07-17 12:46:49 +08:00
f967a24351 chore: pass 2021-07-17 11:43:15 +08:00
259115dea4 feat: init commit __hid 2021-07-17 11:28:09 +08:00
f97124884b feat: gobin 2021-06-12 18:55:18 +08:00
7331163c2b feat: add goblin 2021-06-12 18:33:24 +08:00
85714fd86d feat: add quinn 2021-05-09 00:32:26 +08:00
0328bc17d6 chore: comment 2021-05-05 23:18:19 +08:00
084166e0bd feat: main 2021-05-05 19:55:54 +08:00
355fcc1b9d feat: add build.rs 2021-05-05 19:46:56 +08:00
134c0dc166 feat: add sciter 2021-05-05 19:36:02 +08:00
1addcd26ea feat: add durid 2021-05-01 13:20:30 +08:00
ba796dd652 feat: add native dialog 2021-05-01 12:31:13 +08:00
813a7d5b2f feat: add tide 2021-04-29 23:14:05 +08:00
2b82801053 feat: add hyper_rustls 2021-04-29 22:17:53 +08:00
19a03e85d5 feat: add stream 2021-04-29 21:44:02 +08:00
4bbb7b9578 feat: add tun/tap for mac 2021-04-23 00:45:02 +08:00
5f880886ba feat: update libmacchina 2021-04-18 22:34:46 +08:00
1c58c1cfd2 feat: add libmacchina 2021-04-18 22:29:11 +08:00
d09b72468e feat: add readme 2021-04-11 14:45:05 +08:00
87327c8b11 feat: add iptables 2021-04-11 14:44:24 +08:00
1d02fda724 feat: update readm 2021-04-11 14:07:59 +08:00
ca1fe72030 feat: use buildj :::javac and :::java 2021-04-10 00:30:25 +08:00
f8c96be6a9 feat: robusta_jni 2021-04-10 00:12:55 +08:00
8b2c84b8bc feat: add rocket rs 2021-04-07 00:40:03 +08:00
521fc4f369 feat: add static dir 2021-04-07 00:30:53 +08:00
6bc61da148 feat: add salvo 2021-04-07 00:18:52 +08:00
2dd0c00c03 feat: add regex 2021-04-03 11:47:58 +08:00
a2afc118e2 feat: add gtk 2021-04-02 23:10:31 +08:00
29618a99ec feat: add gtk 2021-04-02 23:10:03 +08:00
1648 changed files with 268260 additions and 443 deletions

172
README.md
View File

@@ -5,70 +5,133 @@ Simple Rust Tests
Project or files: Project or files:
``` ```
. .
├── README_2.md ├── __alloc
│   └── zeroizing-alloc-demo
├── __args
│   ├── argh-test
│   ├── clap-test
│   └── clap-test-2
├── __bin
│   ├── goblin
│   └── object-demo
├── __compress
│   └── zstd-demo
├── __concurrent ├── __concurrent
│   ├── arc-swap │   ├── arc-swap
│   ├── async_study
│   ├── async-fn-resumed-after-completion
│   ├── crossbeam_n_parking_lot │   ├── crossbeam_n_parking_lot
│   ├── glommio │   ├── glommio
│   ├── green-threads │   ├── green-threads
│   ── rayon │   ── qcell
│   ├── rayon
│   └── waitgroup-demo
├── __cpu ├── __cpu
│   └── x86 │   └── x86
├── __crypto ├── __crypto
│   ├── aegis-test
│   ├── aes_ctr_test
│   ├── aes_gcm_crypto
│   ├── asn1-der-test
│   ├── bc1-btc-address
│   ├── btc-address │   ├── btc-address
│   ├── crypto │   ├── crypto
│   ├── crypto2-demo
│   ├── curve25519 │   ├── curve25519
│   ├── efficient_sm2
│   ├── eth-address
│   ├── highwayhash
│   ├── hmac_sha1
│   ├── jose-test
│   ├── jsonwebtoken │   ├── jsonwebtoken
│   ├── liboqs-rust-demo
│   ├── ml-kem-demo
│   ├── okta-bcrypt-issue
│   ├── orion-demo
│   ├── passkey-rs-demo
│   ├── password-hash
│   ├── pkcs8 │   ├── pkcs8
│   ├── pwhash │   ├── pwhash
│   ├── random │   ├── random
│   ├── ring │   ├── ring
│   ├── rpgp-demo
│   ├── rsa │   ├── rsa
│   ├── secp256k1 │   ├── secp256k1
│   ├── secretshare
│   ├── shamir-demo
│   ├── simple_contract │   ├── simple_contract
│   ├── ssh-keys │   ├── ssh-keys
│   ── totp │   ── totp
│   └── yubikey-rs-demo
├── __database ├── __database
│   ├── barrel │   ├── barrel
│   ├── bonsaidb
│   ├── diesel
│   ├── duckdb-demo
│   ├── gluesql │   ├── gluesql
│   ├── mysqlsvr
│   ├── native_db-demo
│   ├── persy │   ├── persy
│   ├── rocksdb │   ├── rocksdb
│   ├── rusqlite │   ├── rusqlite
│   ├── sled │   ├── sled
│   ── sqlparser │   ── sqlite_extersion
│   ├── sqlparser
│   └── sqlx
├── __diff ├── __diff
│   ├── diffy │   ├── diffy
│   ── prettydiff │   ── prettydiff
├── __enclave ├── __enclave
│   └── virt_enclave │   └── virt_enclave
├── __err ├── __err
│   ├── err_derive │   ├── err_derive
│   ── quick_error │   ── quick_error
│   └── thiserror_anyhow
├── __external
│   └── vcard-qr
├── __ffi ├── __ffi
│   ├── abi_stable_crates │   ├── abi_stable_crates
│   ├── c │   ├── c
│   ├── c2
│   ├── c_export │   ├── c_export
│   ├── c_layout │   ├── c_layout
│   ├── c2
│   ├── calltoc
│   ├── cpp │   ├── cpp
│   ├── dotalib │   ├── dotalib
│   ├── jni │   ├── jni
│   ├── live-reload-rust │   ├── live-reload-rust
│   ├── robusta_jni
│   └── rust_link_a │   └── rust_link_a
├── __fs ├── __fs
│   ── fuse │   ── crypt4ghfs-rust
│   ├── fuse
│   ├── fuser
├── __gui ├── __gui
│   ├── druid
│   ├── gtk
│   ├── native-dialog
│   ├── orbtk
│   ├── sciter
│   └── winit │   └── winit
├── __hid
│   └── yubikey_rs
├── __image ├── __image
│   ├── plotters │   ├── plotters
│   └── svg │   └── svg
├── __internal
│   └── memory
├── __lang ├── __lang
│   ├── boa │   ├── boa
│   ├── java
│   ├── monkey-rs
│   ├── peg │   ├── peg
│   ├── pest │   ├── pest
│   ├── quickjs │   ├── quickjs
│   ├── rusty_jsc
│   └── sana │   └── sana
├── __linux ├── __linux
│   ├── ipipe
│   ├── nix-demo
│   ├── notify │   ├── notify
│   ├── psutil │   ├── psutil
│   ├── sysinfo │   ├── sysinfo
@@ -80,81 +143,155 @@ Project or files:
│   └── rusty-machine │   └── rusty-machine
├── __misc ├── __misc
│   ├── better_panic │   ├── better_panic
│   ├── bytes
│   ├── daemonize-demo
│   ├── erasure-coding
│   ├── fancy-regex │   ├── fancy-regex
│   ├── fastqr
│   ├── human_panic │   ├── human_panic
│   ├── icalendar
│   ├── lazy_static │   ├── lazy_static
│   ├── libmacchina
│   ├── num_cpus │   ├── num_cpus
│   ├── obfstr
│   ├── reed-solomon │   ├── reed-solomon
│   ├── regex │   ├── regex
│   ├── shadowrs
│   ├── smartstring │   ├── smartstring
│   ├── uint │   ├── uint
│   └── walkdir │   └── walkdir
├── __network ├── __network
│   ├── async-speed-limit
│   ├── axum-demo
│   ├── dingo
│   ├── dns-server-tutorial
│   ├── fetch-rs
│   ├── fuso-socks5-test
│   ├── html-crawl-parse
│   ├── ip │   ├── ip
│   ├── ipnet-demo
│   ├── iptables
│   ├── kcp-test
│   ├── mailsend
│   ├── message_io
│   ├── n_pcap │   ├── n_pcap
│   ├── nfqueue │   ├── nfqueue
│   ├── ngrok-demo
│   ├── pcap │   ├── pcap
│   ├── pingora-demo
│   ├── quinn
│   ├── rust_tcp
│   ├── rust-rawsock-demo
│   ├── s2n_quic
│   ├── socket2 │   ├── socket2
│   ├── tcp │   ├── tcp
│   ── udp_laminar │   ── tcp-connect-reset
│   ├── tokio-kcp-demo
│   ├── tuntapmac
│   ├── udp_laminar
│   └── ureq
├── __performance ├── __performance
│   └── print_perf │   └── print_perf
├── __regex
│   └── hyperscan
├── __rpc
│   └── dubbo-rust
├── __rule ├── __rule
│   └── json-rules-engine │   └── json-rules-engine
├── __search ├── __search
│   ├── simsearch │   ├── simsearch
│   └── tantivy │   └── tantivy
├── __security
│   ├── keychain-services
│   ├── keyring-demo
│   ├── seckey
│   ├── secmem-proc
│   ├── secrets
│   ├── security-framework
│   └── sensitive
├── __serialization ├── __serialization
│   ├── bytes │   ├── bytes
│   ├── cbor │   ├── cbor
│   ├── configrs │   ├── configrs
│   ├── csv │   ├── csv
│   ├── der_parser_test
│   ├── encoding │   ├── encoding
│   ├── flatbuffers │   ├── flatbuffers
│   ├── gob
│   ├── hjson │   ├── hjson
│   ├── no_proto │   ├── no_proto
│   ├── prost │   ├── prost
│   └── serde_json │   └── serde_json
├── __shell ├── __shell
│   ├── ssh2 │   ├── ssh2
│   ├── sshrs
│   └── xshell │   └── xshell
├── __shuttle
│   ├── hatter-test
├── __std ├── __std
│   ├── async_await │   ├── async_await
│   ├── async-fn
│   ├── into │   ├── into
│   ├── iter │   ├── iter
│   ├── misc
│   ├── ops │   ├── ops
│   ── thread │   ── pi
│   ├── thread
│   └── tokio-cpu-affinity
├── __template ├── __template
│   ├── askama │   ├── askama
│   └── handlebars-rust │   └── handlebars-rust
├── __term ├── __term
│   ├── bunt
│   ├── colored_msg │   ├── colored_msg
│   ├── confy_table │   ├── comfy_table
│   ├── cursive-demo
│   ├── dialoguer-demo
│   ├── indicatif │   ├── indicatif
│   ├── prettyprint
│   ├── rustyline │   ├── rustyline
│   ├── structopt │   ├── structopt
│   ── term │   ── tabled
│   ├── term
│   └── tui-markup-demo
├── __time ├── __time
│   ├── chrono │   ├── chrono
│   ├── humantime-demo
│   └── iron │   └── iron
├── __translate
│   └── retranslate
├── __wasm ├── __wasm
│   ├── deno_rust_wasm_import_functions
│   ├── deno_rust_wasm_js_sandbox
│   ├── deno_rust_wasm_qr_decode
│   ├── extism
│   ├── js-wasm
│   ├── rust-wasm-plugins-examples
│   ├── wasi │   ├── wasi
│   ── wasm │   ── wasm
│   ├── wasmtime
│   ├── wasmtime-serde-demo
│   └── wit-bindgen-sample
├── __web ├── __web
│   ├── actix-web
│   ├── actix_rustls │   ├── actix_rustls
│   ├── actix-web
│   ├── alibabacloundfn │   ├── alibabacloundfn
│   ├── hyper_rustls
│   ├── hyperrs │   ├── hyperrs
│   ├── meter_proxy │   ├── meter_proxy
│   ├── pencil │   ├── pencil
│   ├── reqwest │   ├── reqwest
│   ├── rocketrs
│   ├── rweb │   ├── rweb
│   ├── salvo
│   ├── sincere │   ├── sincere
│   ├── tide
│   └── warp │   └── warp
├── build_readme.rs -> scripts/build_readme_rs/src/main.rs
├── README_2.md
├── scripts ├── scripts
│   ── commit-msg.crs │   ── build_readme_rs
└── single_file_tests └── single_file_tests
├── 99.htm
├── 99.rs ├── 99.rs
├── chain.rs ├── chain.rs
├── closure.rs ├── closure.rs
@@ -177,7 +314,6 @@ Project or files:
├── vec.rs ├── vec.rs
└── while.rs └── while.rs
147 directories, 31 files 286 directories, 38 files
``` ```

View File

@@ -7,5 +7,3 @@ Projects:
* https://github.com/tafia/hyper-proxy * https://github.com/tafia/hyper-proxy
* https://github.com/rusticata/x509-parser * https://github.com/rusticata/x509-parser

16
__alloc/zeroizing-alloc-demo/Cargo.lock generated Normal file
View File

@@ -0,0 +1,16 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "zeroizing-alloc"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ebff5e6b81c1c7dca2d0bd333b2006da48cb37dbcae5a8da888f31fcb3c19934"
[[package]]
name = "zeroizing-alloc-demo"
version = "0.1.0"
dependencies = [
"zeroizing-alloc",
]

View File

@@ -0,0 +1,7 @@
[package]
name = "zeroizing-alloc-demo"
version = "0.1.0"
edition = "2024"
[dependencies]
zeroizing-alloc = "0.1.0"

View File

@@ -0,0 +1,8 @@
use zeroizing_alloc::ZeroAlloc;
#[global_allocator]
static ALLOC: ZeroAlloc<std::alloc::System> = ZeroAlloc(std::alloc::System);
fn main() {
println!("Hello, world!");
}

103
__args/argh-test/Cargo.lock generated Normal file
View File

@@ -0,0 +1,103 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "argh"
version = "0.1.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34ff18325c8a36b82f992e533ece1ec9f9a9db446bd1c14d4f936bac88fcd240"
dependencies = [
"argh_derive",
"argh_shared",
"rust-fuzzy-search",
]
[[package]]
name = "argh-test"
version = "0.1.0"
dependencies = [
"argh",
]
[[package]]
name = "argh_derive"
version = "0.1.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "adb7b2b83a50d329d5d8ccc620f5c7064028828538bdf5646acd60dc1f767803"
dependencies = [
"argh_shared",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "argh_shared"
version = "0.1.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a464143cc82dedcdc3928737445362466b7674b5db4e2eb8e869846d6d84f4f6"
dependencies = [
"serde",
]
[[package]]
name = "proc-macro2"
version = "1.0.92"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.38"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e4dccaaaf89514f546c693ddc140f729f958c247918a13380cccc6078391acc"
dependencies = [
"proc-macro2",
]
[[package]]
name = "rust-fuzzy-search"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a157657054ffe556d8858504af8a672a054a6e0bd9e8ee531059100c0fa11bb2"
[[package]]
name = "serde"
version = "1.0.217"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02fc4265df13d6fa1d00ecff087228cc0a2b5f3c0e87e258d8b94a156e984c70"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.217"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a9bf7cf98d04a2b28aead066b7496853d4779c9cc183c440dbac457641e19a0"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "syn"
version = "2.0.93"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c786062daee0d6db1132800e623df74274a0a87322d8e183338e01b3d98d058"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83"

View File

@@ -0,0 +1,7 @@
[package]
name = "argh-test"
version = "0.1.0"
edition = "2021"
[dependencies]
argh = "0.1.13"

View File

@@ -0,0 +1,22 @@
use argh::FromArgs;
#[derive(Debug, FromArgs)]
/// Reach new heights.
struct GoUp {
/// whether or not to jump
#[argh(switch, short = 'j')]
jump: bool,
/// how high to go
#[argh(option)]
height: usize,
/// an optional nickname for the pilot
#[argh(option)]
pilot_nickname: Option<String>,
}
fn main() {
let up: GoUp = argh::from_env();
println!("{:#?}", up);
}

261
__args/clap-test-2/Cargo.lock generated Normal file
View File

@@ -0,0 +1,261 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "anstream"
version = "0.6.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "301af1932e46185686725e0fad2f8f2aa7da69dd70bf6ecc44d6b703844a3933"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"is_terminal_polyfill",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "862ed96ca487e809f1c8e5a8447f6ee2cf102f846893800b20cebdf541fc6bbd"
[[package]]
name = "anstyle-parse"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c8bdeb6047d8983be085bab0ba1472e6dc604e7041dbf6fcd5e71523014fae9"
dependencies = [
"windows-sys",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "403f75924867bb1033c59fbf0797484329750cfbe3c4325cd33127941fabc882"
dependencies = [
"anstyle",
"once_cell_polyfill",
"windows-sys",
]
[[package]]
name = "clap"
version = "4.5.42"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed87a9d530bb41a67537289bafcac159cb3ee28460e0a4571123d2a778a6a882"
dependencies = [
"clap_builder",
"clap_derive",
]
[[package]]
name = "clap-test-2"
version = "0.1.0"
dependencies = [
"clap",
"clap-verbosity-flag",
]
[[package]]
name = "clap-verbosity-flag"
version = "3.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eeab6a5cdfc795a05538422012f20a5496f050223c91be4e5420bfd13c641fb1"
dependencies = [
"clap",
"log",
]
[[package]]
name = "clap_builder"
version = "4.5.42"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "64f4f3f3c77c94aff3c7e9aac9a2ca1974a5adf392a8bb751e827d6d127ab966"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
]
[[package]]
name = "clap_derive"
version = "4.5.41"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef4f52386a59ca4c860f7393bcf8abd8dfd91ecccc0f774635ff68e92eeef491"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.7.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b94f61472cee1439c0b966b47e3aca9ae07e45d070759512cd390ea2bebc6675"
[[package]]
name = "colorchoice"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75"
[[package]]
name = "heck"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "is_terminal_polyfill"
version = "1.70.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
[[package]]
name = "log"
version = "0.4.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94"
[[package]]
name = "once_cell_polyfill"
version = "1.70.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4895175b425cb1f87721b59f0f286c2092bd4af812243672510e1ac53e2e0ad"
[[package]]
name = "proc-macro2"
version = "1.0.95"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d"
dependencies = [
"proc-macro2",
]
[[package]]
name = "strsim"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
[[package]]
name = "syn"
version = "2.0.104"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17b6f705963418cdb9927482fa304bc562ece2fdd4f616084c50b7023b435a40"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"
[[package]]
name = "utf8parse"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]]
name = "windows-sys"
version = "0.59.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_gnullvm",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
[[package]]
name = "windows_i686_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
[[package]]
name = "windows_i686_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
[[package]]
name = "windows_i686_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"

View File

@@ -0,0 +1,8 @@
[package]
name = "clap-test-2"
version = "0.1.0"
edition = "2024"
[dependencies]
clap = { version = "4.5.42", features = ["derive", "env"] }
clap-verbosity-flag = "3"

View File

@@ -0,0 +1,158 @@
use clap::Parser;
// https://github.com/fastn-stack/kulfi/blob/main/malai/src/main.rs
fn main() {
let cli = Cli::parse();
println!("{:?}", cli);
}
#[derive(clap::Parser, Debug)]
#[command(version, about, long_about = None)]
pub struct Cli {
#[command(flatten)]
verbose: clap_verbosity_flag::Verbosity,
#[command(subcommand)]
pub command: Option<Command>,
// adding these two because when we run `cargo tauri dev,` it automatically passes these
// arguments. need to figure out why and how to disable that, till then this is a workaround
#[arg(default_value = "true", long, hide = true)]
no_default_features: bool,
#[arg(default_value = "auto", long, hide = true)]
color: String,
}
#[derive(clap::Subcommand, Debug)]
pub enum Command {
// TODO: add this to the docs when we have ACL
// By default it allows any peer to connect to the HTTP(s) service. You can pass --what-to-do
// argument to specify a What To Do service that can be used to add access control."
#[clap(about = "Expose HTTP Service on kulfi, connect using kulfi or browser")]
Http {
port: u16,
#[arg(
long,
default_value = "127.0.0.1",
help = "Host serving the http service."
)]
host: String,
#[arg(
long,
default_value = "kulfi.site",
help = "Use this for the HTTP bridge. To run an HTTP bridge, use `malai http-bridge`",
env = "MALAI_HTTP_BRIDGE"
)]
bridge: String,
#[arg(
long,
help = "Make the exposed service public. Anyone will be able to access."
)]
public: bool,
// #[arg(
// long,
// default_value_t = false,
// help = "Use this if the service is HTTPS"
// )]
// secure: bool,
// #[arg(
// long,
// help = "The What To Do Service that can be used to add access control."
// )]
// this will be the id52 of the identity server that should be consulted
// what_to_do: Option<String>,
},
#[clap(about = "Browse a kulfi site.")]
Browse {
#[arg(help = "The Kulfi URL to browse. Should look like kulfi://<id52>/<path>")]
url: String,
},
#[clap(about = "Expose TCP Service on kulfi.")]
Tcp {
port: u16,
#[arg(
long,
default_value = "127.0.0.1",
help = "Host serving the TCP service."
)]
host: String,
#[arg(
long,
help = "Make the exposed service public. Anyone will be able to access."
)]
public: bool,
},
#[clap(
about = "Run an http server that forwards requests to the given id52 taken from the HOST header"
)]
HttpBridge {
#[arg(
long,
short('t'),
help = "The id52 to which this bridge will forward incoming HTTP request. By default it forwards to every id52."
)]
proxy_target: Option<String>,
#[arg(
long,
short('p'),
help = "The port on which this bridge will listen for incoming HTTP requests. If you pass 0, it will bind to a random port.",
default_value = "0"
)]
port: u16,
},
#[clap(about = "Run a TCP server that forwards incoming requests to the given id52.")]
TcpBridge {
#[arg(help = "The id52 to which this bridge will forward incoming TCP request.")]
proxy_target: String,
#[arg(
help = "The port on which this bridge will listen for incoming TCP requests. If you pass 0, it will bind to a random port.",
default_value = "0"
)]
port: u16,
},
#[clap(about = "Expose a folder to kulfi network")]
Folder {
#[arg(help = "The folder to expose.")]
path: String,
#[arg(
long,
default_value = "kulfi.site",
help = "Use this for the HTTP bridge. To run an HTTP bridge, use `malai http-bridge`",
env = "MALAI_HTTP_BRIDGE"
)]
bridge: String,
#[arg(long, help = "Make the folder public. Anyone will be able to access.")]
public: bool,
},
#[clap(about = "Run all the services")]
Run {
#[arg(long, help = "Malai Home", env = "MALAI_HOME")]
home: Option<String>,
},
#[clap(about = "Run an iroh remote server that handles requests from http-proxy.")]
HttpProxyRemote {
#[arg(long, help = "Make the proxy public. Anyone will be able to access.")]
public: bool,
},
#[clap(about = "Run a http proxy server that forwards incoming requests to http-proxy-remote.")]
HttpProxy {
#[arg(help = "The id52 of remote to which this http proxy will forward request to.")]
remote: String,
#[arg(
help = "The port on which this proxy will listen for incoming TCP requests. If you pass 0, it will bind to a random port.",
default_value = "0"
)]
port: u16,
},
#[clap(about = "Generate a new identity.")]
Keygen {
#[arg(
long,
short,
num_args=0..=1,
//default_missing_value=kulfi_utils::SECRET_KEY_FILE,
help = "The file where the private key of the identity will be stored. If not provided, the private key will be printed to stdout."
)]
file: Option<String>,
},
}

237
__args/clap-test/Cargo.lock generated Normal file
View File

@@ -0,0 +1,237 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "anstream"
version = "0.6.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8acc5369981196006228e28809f761875c0327210a891e941f4c683b3a99529b"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"is_terminal_polyfill",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9"
[[package]]
name = "anstyle-parse"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b2d16507662817a6a20a9ea92df6652ee4f94f914589377d69f3b21bc5798a9"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "79947af37f4177cfead1110013d678905c37501914fba0efea834c3fe9a8d60c"
dependencies = [
"windows-sys",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2109dbce0e72be3ec00bed26e6a7479ca384ad226efdd66db8fa2e3a38c83125"
dependencies = [
"anstyle",
"windows-sys",
]
[[package]]
name = "clap"
version = "4.5.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3135e7ec2ef7b10c6ed8950f0f792ed96ee093fa088608f1c76e569722700c84"
dependencies = [
"clap_builder",
"clap_derive",
]
[[package]]
name = "clap-test"
version = "0.1.0"
dependencies = [
"clap",
]
[[package]]
name = "clap_builder"
version = "4.5.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30582fc632330df2bd26877bde0c1f4470d57c582bbc070376afcd04d8cb4838"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
]
[[package]]
name = "clap_derive"
version = "4.5.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ac6a0c7b1a9e9a5186361f67dfa1b88213572f427fb9ab038efb2bd8c582dab"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6"
[[package]]
name = "colorchoice"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990"
[[package]]
name = "heck"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "is_terminal_polyfill"
version = "1.70.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
[[package]]
name = "proc-macro2"
version = "1.0.92"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.38"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e4dccaaaf89514f546c693ddc140f729f958c247918a13380cccc6078391acc"
dependencies = [
"proc-macro2",
]
[[package]]
name = "strsim"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
[[package]]
name = "syn"
version = "2.0.93"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c786062daee0d6db1132800e623df74274a0a87322d8e183338e01b3d98d058"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83"
[[package]]
name = "utf8parse"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]]
name = "windows-sys"
version = "0.59.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_gnullvm",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
[[package]]
name = "windows_i686_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
[[package]]
name = "windows_i686_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
[[package]]
name = "windows_i686_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"

View File

@@ -0,0 +1,7 @@
[package]
name = "clap-test"
version = "0.1.0"
edition = "2021"
[dependencies]
clap = { version = "4.5.23", features = ["derive"] }

View File

@@ -0,0 +1,165 @@
use std::ffi::OsStr;
use std::ffi::OsString;
use std::path::PathBuf;
use clap::{Args, Parser, Subcommand, ValueEnum};
/// A fictional versioning CLI
#[derive(Debug, Parser)] // requires `derive` feature
#[command(name = "git")]
#[command(about = "A fictional versioning CLI", long_about = None)]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Debug, Subcommand)]
enum Commands {
/// Clones repos
#[command(arg_required_else_help = true)]
Clone {
/// The remote to clone
remote: String,
},
/// Compare two commits
Diff {
#[arg(value_name = "COMMIT")]
base: Option<OsString>,
#[arg(value_name = "COMMIT")]
head: Option<OsString>,
#[arg(last = true)]
path: Option<OsString>,
#[arg(
long,
require_equals = true,
value_name = "WHEN",
num_args = 0..=1,
default_value_t = ColorWhen::Auto,
default_missing_value = "always",
value_enum
)]
color: ColorWhen,
},
/// pushes things
#[command(arg_required_else_help = true)]
Push {
/// The remote to target
remote: String,
},
/// adds things
#[command(arg_required_else_help = true)]
Add {
/// Stuff to add
#[arg(required = true)]
path: Vec<PathBuf>,
},
Stash(StashArgs),
#[command(external_subcommand)]
External(Vec<OsString>),
}
#[derive(ValueEnum, Copy, Clone, Debug, PartialEq, Eq)]
enum ColorWhen {
Always,
Auto,
Never,
}
impl std::fmt::Display for ColorWhen {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.to_possible_value()
.expect("no values are skipped")
.get_name()
.fmt(f)
}
}
#[derive(Debug, Args)]
#[command(args_conflicts_with_subcommands = true)]
#[command(flatten_help = true)]
struct StashArgs {
#[command(subcommand)]
command: Option<StashCommands>,
#[command(flatten)]
push: StashPushArgs,
}
#[derive(Debug, Subcommand)]
enum StashCommands {
Push(StashPushArgs),
Pop { stash: Option<String> },
Apply { stash: Option<String> },
}
#[derive(Debug, Args)]
struct StashPushArgs {
#[arg(short, long)]
message: Option<String>,
}
fn main() {
let args = Cli::parse();
match args.command {
Commands::Clone { remote } => {
println!("Cloning {remote}");
}
Commands::Diff {
mut base,
mut head,
mut path,
color,
} => {
if path.is_none() {
path = head;
head = None;
if path.is_none() {
path = base;
base = None;
}
}
let base = base
.as_deref()
.map(|s| s.to_str().unwrap())
.unwrap_or("stage");
let head = head
.as_deref()
.map(|s| s.to_str().unwrap())
.unwrap_or("worktree");
let path = path.as_deref().unwrap_or_else(|| OsStr::new(""));
println!(
"Diffing {}..{} {} (color={})",
base,
head,
path.to_string_lossy(),
color
);
}
Commands::Push { remote } => {
println!("Pushing to {remote}");
}
Commands::Add { path } => {
println!("Adding {path:?}");
}
Commands::Stash(stash) => {
let stash_cmd = stash.command.unwrap_or(StashCommands::Push(stash.push));
match stash_cmd {
StashCommands::Push(push) => {
println!("Pushing {push:?}");
}
StashCommands::Pop { stash } => {
println!("Popping {stash:?}");
}
StashCommands::Apply { stash } => {
println!("Applying {stash:?}");
}
}
}
Commands::External(args) => {
println!("Calling out to {:?} with {:?}", &args[0], &args[1..]);
}
}
// Continued program logic goes here...
}

232
__bin/goblin/Cargo.lock generated Normal file
View File

@@ -0,0 +1,232 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "bitflags"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "dirs-next"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b98cf8ebf19c3d1b223e151f99a4f9f0690dca41414773390fc824184ac833e1"
dependencies = [
"cfg-if",
"dirs-sys-next",
]
[[package]]
name = "dirs-sys-next"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ebda144c4fe02d1f7ea1a7d9641b6fc6b580adcfa024ae48797ecdeb6825b4d"
dependencies = [
"libc",
"redox_users",
"winapi",
]
[[package]]
name = "getrandom"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fcd999463524c52659517fe2cea98493cfe485d10565e7b0fb07dbba7ad2753"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "goblin"
version = "0.1.0"
dependencies = [
"goblin 0.4.1",
"rust_util",
]
[[package]]
name = "goblin"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee05c709047abe5eb0571880d2887ac77299c5f0835f8e6b7f4d5404f8962921"
dependencies = [
"log",
"plain",
"scroll",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.97"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "12b8adadd720df158f4d70dfe7ccc6adb0472d7c55ca83445f6a5ab3e36f8fb6"
[[package]]
name = "log"
version = "0.4.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710"
dependencies = [
"cfg-if",
]
[[package]]
name = "plain"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4596b6d070b27117e987119b4dac604f3c58cfb0b191112e24771b2faeac1a6"
[[package]]
name = "proc-macro2"
version = "1.0.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0d8caf72986c1a598726adc988bb5984792ef84f5ee5aa50209145ee8077038"
dependencies = [
"unicode-xid",
]
[[package]]
name = "quote"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7"
dependencies = [
"proc-macro2",
]
[[package]]
name = "redox_syscall"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "742739e41cd49414de871ea5e549afb7e2a3ac77b589bcbebe8c82fab37147fc"
dependencies = [
"bitflags",
]
[[package]]
name = "redox_users"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "528532f3d801c87aec9def2add9ca802fe569e44a544afe633765267840abe64"
dependencies = [
"getrandom",
"redox_syscall",
]
[[package]]
name = "rust_util"
version = "0.6.39"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "21b1ebfe593930e71805e37c914bf2dc48c2194b9dff7f0f1af8c59f92c5cff5"
dependencies = [
"lazy_static",
"libc",
"term",
"term_size",
]
[[package]]
name = "rustversion"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61b3909d758bb75c79f23d4736fac9433868679d3ad2ea7a61e3c25cfda9a088"
[[package]]
name = "scroll"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fda28d4b4830b807a8b43f7b0e6b5df875311b3e7621d84577188c175b6ec1ec"
dependencies = [
"scroll_derive",
]
[[package]]
name = "scroll_derive"
version = "0.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aaaae8f38bb311444cfb7f1979af0bc9240d95795f75f9ceddf6a59b79ceffa0"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "syn"
version = "1.0.73"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f71489ff30030d2ae598524f61326b902466f72a0fb1a8564c001cc63425bcc7"
dependencies = [
"proc-macro2",
"quote",
"unicode-xid",
]
[[package]]
name = "term"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c59df8ac95d96ff9bede18eb7300b0fda5e5d8d90960e76f8e14ae765eedbf1f"
dependencies = [
"dirs-next",
"rustversion",
"winapi",
]
[[package]]
name = "term_size"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e4129646ca0ed8f45d09b929036bafad5377103edd06e50bf574b353d2b08d9"
dependencies = [
"libc",
"winapi",
]
[[package]]
name = "unicode-xid"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
[[package]]
name = "wasi"
version = "0.10.2+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

12
__bin/goblin/Cargo.toml Normal file
View File

@@ -0,0 +1,12 @@
[package]
name = "goblin"
version = "0.1.0"
authors = ["Hatter Jiang@Pixelbook <jht5945@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rust_util = "0.6.39"
goblin = "0.4.1"

32
__bin/goblin/src/main.rs Normal file
View File

@@ -0,0 +1,32 @@
#[macro_use] extern crate rust_util;
use std::path::Path;
use goblin::Object;
fn main() {
let file = std::env::args().nth(1);
match file {
None => failure_and_exit!("Must assign a file"),
Some(f) => {
let path = Path::new(&f);
let buff = std::fs::read(path).unwrap_or_else(|e| failure_and_exit!("Read file: {}, failed: {}", f, e));
match Object::parse(&buff).unwrap_or_else(|e| failure_and_exit!("Read file: {}, failed: {}", f , e)) {
Object::Elf(elf) => {
success!("Found elf file: {:?}", elf.header);
}
Object::PE(pe) => {
success!("Found PE file: {:?}", pe);
}
Object::Mach(mach) => {
success!("Found mach file: {:?}", mach);
}
Object::Archive(archive) => {
success!("Fund linux archive file: {:?}", archive);
}
Object::Unknown(magic) => {
failure!("Unknown file, magic: {}", magic);
}
}
}
}
}

82
__bin/object-demo/Cargo.lock generated Normal file
View File

@@ -0,0 +1,82 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "adler"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "crc32fast"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d"
dependencies = [
"cfg-if",
]
[[package]]
name = "flate2"
version = "1.0.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8a2db397cb1c8772f31494cb8917e48cd1e64f0fa7efac59fbd741a0a8ce841"
dependencies = [
"crc32fast",
"miniz_oxide",
]
[[package]]
name = "libc"
version = "0.2.140"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99227334921fae1a979cf0bfdfcc6b3e5ce376ef57e16fb6fb3ea2ed6095f80c"
[[package]]
name = "memchr"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
[[package]]
name = "memmap2"
version = "0.5.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "83faa42c0a078c393f6b29d5db232d8be22776a891f8f56e5284faee4a20b327"
dependencies = [
"libc",
]
[[package]]
name = "miniz_oxide"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b275950c28b37e794e8c55d88aeb5e139d0ce23fdbbeda68f8d7174abdf9e8fa"
dependencies = [
"adler",
]
[[package]]
name = "object"
version = "0.30.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea86265d3d3dcb6a27fc51bd29a4bf387fae9d2986b823079d4986af253eb439"
dependencies = [
"flate2",
"memchr",
]
[[package]]
name = "object-demo"
version = "0.1.0"
dependencies = [
"memmap2",
"object",
]

View File

@@ -0,0 +1,10 @@
[package]
name = "object-demo"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
memmap2 = "0.5.10"
object = "0.30.3"

View File

@@ -0,0 +1,323 @@
use std::io::{Result, Write};
use object::{Endianness, Object, ObjectComdat, ObjectSection, ObjectSymbol};
use object::read::archive::ArchiveFile;
use object::read::macho::{DyldCache, FatArch, FatHeader};
use std::{env, fs, io, process};
fn main() {
let mut args = env::args();
let cmd = args.next().unwrap();
if args.len() == 0 {
eprintln!("Usage: {} <file> [<member>...]", cmd);
process::exit(1);
}
let file_path = args.next().unwrap();
let member_names: Vec<_> = args.collect();
let file = match fs::File::open(&file_path) {
Ok(file) => file,
Err(err) => {
eprintln!("Failed to open file '{}': {}", file_path, err,);
process::exit(1);
}
};
let extra_files = open_subcaches_if_exist(&file_path);
let file = match unsafe { memmap2::Mmap::map(&file) } {
Ok(mmap) => mmap,
Err(err) => {
eprintln!("Failed to map file '{}': {}", file_path, err,);
process::exit(1);
}
};
let extra_files: Vec<_> = extra_files
.into_iter()
.map(
|subcache_file| match unsafe { memmap2::Mmap::map(&subcache_file) } {
Ok(mmap) => mmap,
Err(err) => {
eprintln!("Failed to map file '{}': {}", file_path, err,);
process::exit(1);
}
},
)
.collect();
let extra_file_data: Vec<&[u8]> = extra_files.iter().map(|f| &**f).collect();
let stdout = io::stdout();
let stderr = io::stderr();
print(
&mut stdout.lock(),
&mut stderr.lock(),
&*file,
&extra_file_data,
member_names,
)
.unwrap();
}
// If the file is a dyld shared cache, and we're on macOS 12 or later,
// then there will be one or more "subcache" files next to this file,
// with the names filename.1, filename.2 etc.
// Read those files now, if they exist, even if we don't know that
// we're dealing with a dyld shared cache. By the time we know what
// we're dealing with, it's too late to read more files.
fn open_subcaches_if_exist(path: &str) -> Vec<fs::File> {
let mut files = Vec::new();
for i in 1.. {
let subcache_path = format!("{}.{}", path, i);
match fs::File::open(&subcache_path) {
Ok(subcache_file) => files.push(subcache_file),
Err(_) => break,
};
}
let symbols_subcache_path = format!("{}.symbols", path);
if let Ok(subcache_file) = fs::File::open(&symbols_subcache_path) {
files.push(subcache_file);
};
files
}
pub fn print<W: Write, E: Write>(
w: &mut W,
e: &mut E,
file: &[u8],
extra_files: &[&[u8]],
member_names: Vec<String>,
) -> Result<()> {
let mut member_names: Vec<_> = member_names.into_iter().map(|name| (name, false)).collect();
if let Ok(archive) = ArchiveFile::parse(file) {
writeln!(w, "Format: Archive (kind: {:?})", archive.kind())?;
for member in archive.members() {
match member {
Ok(member) => {
if find_member(&mut member_names, member.name()) {
writeln!(w)?;
writeln!(w, "{}:", String::from_utf8_lossy(member.name()))?;
if let Ok(data) = member.data(file) {
dump_object(w, e, data)?;
}
}
}
Err(err) => writeln!(e, "Failed to parse archive member: {}", err)?,
}
}
} else if let Ok(arches) = FatHeader::parse_arch32(file) {
writeln!(w, "Format: Mach-O Fat 32")?;
for arch in arches {
writeln!(w)?;
writeln!(w, "Fat Arch: {:?}", arch.architecture())?;
match arch.data(file) {
Ok(data) => dump_object(w, e, data)?,
Err(err) => writeln!(e, "Failed to parse Fat 32 data: {}", err)?,
}
}
} else if let Ok(arches) = FatHeader::parse_arch64(file) {
writeln!(w, "Format: Mach-O Fat 64")?;
for arch in arches {
writeln!(w)?;
writeln!(w, "Fat Arch: {:?}", arch.architecture())?;
match arch.data(file) {
Ok(data) => dump_object(w, e, data)?,
Err(err) => writeln!(e, "Failed to parse Fat 64 data: {}", err)?,
}
}
} else if let Ok(cache) = DyldCache::<Endianness>::parse(file, extra_files) {
writeln!(w, "Format: dyld cache {:?}-endian", cache.endianness())?;
writeln!(w, "Architecture: {:?}", cache.architecture())?;
for image in cache.images() {
let path = match image.path() {
Ok(path) => path,
Err(err) => {
writeln!(e, "Failed to parse dydld image name: {}", err)?;
continue;
}
};
if !find_member(&mut member_names, path.as_bytes()) {
continue;
}
writeln!(w)?;
writeln!(w, "{}:", path)?;
let file = match image.parse_object() {
Ok(file) => file,
Err(err) => {
writeln!(e, "Failed to parse file: {}", err)?;
continue;
}
};
dump_parsed_object(w, e, &file)?;
}
} else {
dump_object(w, e, file)?;
}
for (name, found) in member_names {
if !found {
writeln!(e, "Failed to find member '{}", name)?;
}
}
Ok(())
}
fn find_member(member_names: &mut [(String, bool)], name: &[u8]) -> bool {
if member_names.is_empty() {
return true;
}
match member_names.iter().position(|x| x.0.as_bytes() == name) {
Some(i) => {
member_names[i].1 = true;
true
}
None => false,
}
}
fn dump_object<W: Write, E: Write>(w: &mut W, e: &mut E, data: &[u8]) -> Result<()> {
match object::File::parse(data) {
Ok(file) => {
dump_parsed_object(w, e, &file)?;
}
Err(err) => {
writeln!(e, "Failed to parse file: {}", err)?;
}
}
Ok(())
}
fn dump_parsed_object<W: Write, E: Write>(w: &mut W, e: &mut E, file: &object::File) -> Result<()> {
writeln!(
w,
"Format: {:?} {:?}-endian {}-bit",
file.format(),
file.endianness(),
if file.is_64() { "64" } else { "32" }
)?;
writeln!(w, "Kind: {:?}", file.kind())?;
writeln!(w, "Architecture: {:?}", file.architecture())?;
writeln!(w, "Flags: {:x?}", file.flags())?;
writeln!(
w,
"Relative Address Base: {:x?}",
file.relative_address_base()
)?;
writeln!(w, "Entry Address: {:x?}", file.entry())?;
match file.mach_uuid() {
Ok(Some(uuid)) => writeln!(w, "Mach UUID: {:x?}", uuid)?,
Ok(None) => {}
Err(err) => writeln!(e, "Failed to parse Mach UUID: {}", err)?,
}
match file.build_id() {
Ok(Some(build_id)) => writeln!(w, "Build ID: {:x?}", build_id)?,
Ok(None) => {}
Err(err) => writeln!(e, "Failed to parse build ID: {}", err)?,
}
match file.gnu_debuglink() {
Ok(Some((filename, crc))) => writeln!(
w,
"GNU debug link: {} CRC: {:08x}",
String::from_utf8_lossy(filename),
crc,
)?,
Ok(None) => {}
Err(err) => writeln!(e, "Failed to parse GNU debug link: {}", err)?,
}
match file.gnu_debugaltlink() {
Ok(Some((filename, build_id))) => writeln!(
w,
"GNU debug alt link: {}, build ID: {:x?}",
String::from_utf8_lossy(filename),
build_id,
)?,
Ok(None) => {}
Err(err) => writeln!(e, "Failed to parse GNU debug alt link: {}", err)?,
}
match file.pdb_info() {
Ok(Some(info)) => writeln!(
w,
"PDB file: {}, GUID: {:x?}, Age: {}",
String::from_utf8_lossy(info.path()),
info.guid(),
info.age()
)?,
Ok(None) => {}
Err(err) => writeln!(e, "Failed to parse PE CodeView info: {}", err)?,
}
for segment in file.segments() {
writeln!(w, "{:x?}", segment)?;
}
for section in file.sections() {
writeln!(w, "{}: {:x?}", section.index().0, section)?;
}
for comdat in file.comdats() {
write!(w, "{:?} Sections:", comdat)?;
for section in comdat.sections() {
write!(w, " {}", section.0)?;
}
writeln!(w)?;
}
writeln!(w)?;
writeln!(w, "Symbols")?;
for symbol in file.symbols() {
writeln!(w, "{}: {:x?}", symbol.index().0, symbol)?;
}
for section in file.sections() {
if section.relocations().next().is_some() {
writeln!(
w,
"\n{} relocations",
section.name().unwrap_or("<invalid name>")
)?;
for relocation in section.relocations() {
writeln!(w, "{:x?}", relocation)?;
}
}
}
writeln!(w)?;
writeln!(w, "Dynamic symbols")?;
for symbol in file.dynamic_symbols() {
writeln!(w, "{}: {:x?}", symbol.index().0, symbol)?;
}
if let Some(relocations) = file.dynamic_relocations() {
writeln!(w)?;
writeln!(w, "Dynamic relocations")?;
for relocation in relocations {
writeln!(w, "{:x?}", relocation)?;
}
}
match file.imports() {
Ok(imports) => {
if !imports.is_empty() {
writeln!(w)?;
for import in imports {
writeln!(w, "{:x?}", import)?;
}
}
}
Err(err) => writeln!(e, "Failed to parse imports: {}", err)?,
}
match file.exports() {
Ok(exports) => {
if !exports.is_empty() {
writeln!(w)?;
for export in exports {
writeln!(w, "{:x?}", export)?;
}
}
}
Err(err) => writeln!(e, "Failed to parse exports: {}", err)?,
}
Ok(())
}

70
__compress/zstd-demo/Cargo.lock generated Normal file
View File

@@ -0,0 +1,70 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "cc"
version = "1.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f"
dependencies = [
"jobserver",
]
[[package]]
name = "jobserver"
version = "0.1.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "068b1ee6743e4d11fb9c6a1e6064b3693a1b600e7f5f5988047d98b3dc9fb90b"
dependencies = [
"libc",
]
[[package]]
name = "libc"
version = "0.2.139"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79"
[[package]]
name = "pkg-config"
version = "0.3.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ac9a59f73473f1b8d852421e59e64809f025994837ef743615c6d0c5b305160"
[[package]]
name = "zstd"
version = "0.12.3+zstd.1.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "76eea132fb024e0e13fd9c2f5d5d595d8a967aa72382ac2f9d39fcc95afd0806"
dependencies = [
"zstd-safe",
]
[[package]]
name = "zstd-demo"
version = "0.1.0"
dependencies = [
"zstd",
]
[[package]]
name = "zstd-safe"
version = "6.0.4+zstd.1.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7afb4b54b8910cf5447638cb54bf4e8a65cbedd783af98b98c62ffe91f185543"
dependencies = [
"libc",
"zstd-sys",
]
[[package]]
name = "zstd-sys"
version = "2.0.7+zstd.1.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94509c3ba2fe55294d752b79842c530ccfab760192521df74a081a78d2b3c7f5"
dependencies = [
"cc",
"libc",
"pkg-config",
]

View File

@@ -0,0 +1,9 @@
[package]
name = "zstd-demo"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
zstd = "0.12.3"

View File

@@ -0,0 +1,29 @@
use std::collections::VecDeque;
use std::io;
use zstd::Encoder;
fn main() {
let inputs = "hello world".repeat(100);
let outputs = compress_manually(1, inputs.as_bytes().to_vec().into());
println!("Inputs len: {}", inputs.len());
println!("Outputs len: {}", outputs.len());
println!("{:x?}", outputs);
}
// fn compress(level: i32) {
// zstd::stream::copy_encode(io::stdin(), io::stdout(), level).unwrap();
// }
// fn decompress() {
// zstd::stream::copy_decode(io::stdin(), io::stdout()).unwrap();
// }
// This function does the same thing, directly using an `Encoder`:
fn compress_manually(level: i32, mut input: VecDeque<u8>) -> Vec<u8> {
let mut output: Vec<u8> = vec![];
let mut encoder = Encoder::new(&mut output, level).unwrap();
io::copy(&mut input, &mut encoder).unwrap();
encoder.finish().unwrap();
output
}

View File

@@ -0,0 +1,421 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "addr2line"
version = "0.21.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a30b2e23b9e17a9f90641c7ab1549cd9b44f296d3ccbf309d2863cfe398a0cb"
dependencies = [
"gimli",
]
[[package]]
name = "adler"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "async-fn-resumed-after-completion"
version = "0.1.0"
dependencies = [
"tokio",
]
[[package]]
name = "autocfg"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "backtrace"
version = "0.3.69"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2089b7e3f35b9dd2d0ed921ead4f6d318c27680d4a5bd167b3ee120edb105837"
dependencies = [
"addr2line",
"cc",
"cfg-if",
"libc",
"miniz_oxide",
"object",
"rustc-demangle",
]
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bytes"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2bd12c1caf447e69cd4528f47f94d203fd2582878ecb9e9465484c4148a8223"
[[package]]
name = "cc"
version = "1.0.90"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8cd6604a82acf3039f1144f54b8eb34e91ffba622051189e71b781822d5ee1f5"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "gimli"
version = "0.28.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253"
[[package]]
name = "hermit-abi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024"
[[package]]
name = "libc"
version = "0.2.153"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd"
[[package]]
name = "lock_api"
version = "0.4.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c168f8615b12bc01f9c17e2eb0cc07dcae1940121185446edc3744920e8ef45"
dependencies = [
"autocfg",
"scopeguard",
]
[[package]]
name = "memchr"
version = "2.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149"
[[package]]
name = "miniz_oxide"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d811f3e15f28568be3407c8e7fdb6514c1cda3cb30683f15b6a1a1dc4ea14a7"
dependencies = [
"adler",
]
[[package]]
name = "mio"
version = "0.8.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4a650543ca06a924e8b371db273b2756685faae30f8487da1b56505a8f78b0c"
dependencies = [
"libc",
"wasi",
"windows-sys 0.48.0",
]
[[package]]
name = "num_cpus"
version = "1.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43"
dependencies = [
"hermit-abi",
"libc",
]
[[package]]
name = "object"
version = "0.32.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6a622008b6e321afc04970976f62ee297fdbaa6f95318ca343e3eebb9648441"
dependencies = [
"memchr",
]
[[package]]
name = "parking_lot"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f"
dependencies = [
"lock_api",
"parking_lot_core",
]
[[package]]
name = "parking_lot_core"
version = "0.9.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c42a9226546d68acdd9c0a280d17ce19bfe27a46bf68784e4066115788d008e"
dependencies = [
"cfg-if",
"libc",
"redox_syscall",
"smallvec",
"windows-targets 0.48.5",
]
[[package]]
name = "pin-project-lite"
version = "0.2.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58"
[[package]]
name = "proc-macro2"
version = "1.0.78"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef"
dependencies = [
"proc-macro2",
]
[[package]]
name = "redox_syscall"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4722d768eff46b75989dd134e5c353f0d6296e5aaa3132e776cbdb56be7731aa"
dependencies = [
"bitflags",
]
[[package]]
name = "rustc-demangle"
version = "0.1.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76"
[[package]]
name = "scopeguard"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
[[package]]
name = "signal-hook-registry"
version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d8229b473baa5980ac72ef434c4415e70c4b5e71b423043adb4ba059f89c99a1"
dependencies = [
"libc",
]
[[package]]
name = "smallvec"
version = "1.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6ecd384b10a64542d77071bd64bd7b231f4ed5940fba55e98c3de13824cf3d7"
[[package]]
name = "socket2"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05ffd9c0a93b7543e062e759284fcf5f5e3b098501104bfbdde4d404db792871"
dependencies = [
"libc",
"windows-sys 0.52.0",
]
[[package]]
name = "syn"
version = "2.0.52"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b699d15b36d1f02c3e7c69f8ffef53de37aefae075d8488d4ba1a7788d574a07"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "tokio"
version = "1.36.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61285f6515fa018fb2d1e46eb21223fff441ee8db5d0f1435e8ab4f5cdb80931"
dependencies = [
"backtrace",
"bytes",
"libc",
"mio",
"num_cpus",
"parking_lot",
"pin-project-lite",
"signal-hook-registry",
"socket2",
"tokio-macros",
"windows-sys 0.48.0",
]
[[package]]
name = "tokio-macros"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "unicode-ident"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "windows-sys"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
dependencies = [
"windows-targets 0.48.5",
]
[[package]]
name = "windows-sys"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
dependencies = [
"windows-targets 0.52.4",
]
[[package]]
name = "windows-targets"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
dependencies = [
"windows_aarch64_gnullvm 0.48.5",
"windows_aarch64_msvc 0.48.5",
"windows_i686_gnu 0.48.5",
"windows_i686_msvc 0.48.5",
"windows_x86_64_gnu 0.48.5",
"windows_x86_64_gnullvm 0.48.5",
"windows_x86_64_msvc 0.48.5",
]
[[package]]
name = "windows-targets"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7dd37b7e5ab9018759f893a1952c9420d060016fc19a472b4bb20d1bdd694d1b"
dependencies = [
"windows_aarch64_gnullvm 0.52.4",
"windows_aarch64_msvc 0.52.4",
"windows_i686_gnu 0.52.4",
"windows_i686_msvc 0.52.4",
"windows_x86_64_gnu 0.52.4",
"windows_x86_64_gnullvm 0.52.4",
"windows_x86_64_msvc 0.52.4",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bcf46cf4c365c6f2d1cc93ce535f2c8b244591df96ceee75d8e83deb70a9cac9"
[[package]]
name = "windows_aarch64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da9f259dd3bcf6990b55bffd094c4f7235817ba4ceebde8e6d11cd0c5633b675"
[[package]]
name = "windows_i686_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
[[package]]
name = "windows_i686_gnu"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b474d8268f99e0995f25b9f095bc7434632601028cf86590aea5c8a5cb7801d3"
[[package]]
name = "windows_i686_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
[[package]]
name = "windows_i686_msvc"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1515e9a29e5bed743cb4415a9ecf5dfca648ce85ee42e15873c3cd8610ff8e02"
[[package]]
name = "windows_x86_64_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5eee091590e89cc02ad514ffe3ead9eb6b660aedca2183455434b93546371a03"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77ca79f2451b49fa9e2af39f0747fe999fcda4f5e241b2898624dca97a1f2177"
[[package]]
name = "windows_x86_64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32b752e52a2da0ddfbdbcc6fceadfeede4c939ed16d13e648833a61dfb611ed8"

View File

@@ -0,0 +1,9 @@
[package]
name = "async-fn-resumed-after-completion"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
tokio = { version = "1.36.0", features = ["full"] }

View File

@@ -0,0 +1,48 @@
async fn action(input: Option<i32>) -> Option<String> {
// 若 input输入是None则返回 None
// 事实上也可以这么写: `let i = input?;`
let _i = match input {
Some(input) => input,
None => return None,
};
// 这里定义一些逻辑
Some("test".into())
}
#[tokio::main]
async fn main() {
let (tx, mut rx) = tokio::sync::mpsc::channel(128);
let mut done = false;
let operation = action(None);
tokio::pin!(operation);
tokio::spawn(async move {
let _ = tx.send(1).await;
let _ = tx.send(3).await;
let _ = tx.send(2).await;
});
loop {
tokio::select! {
res = &mut operation, if !done => {
println!("res = &mut operation, done={}", done);
done = true;
if let Some(v) = res {
println!("GOT = {}", v);
return;
}
}
Some(v) = rx.recv() => {
println!("Some(v={}) = rx.recv()", v);
if v % 2 == 0 {
// `.set` 是 `Pin` 上定义的方法
operation.set(action(Some(v)));
done = false;
}
}
}
}
}

438
__concurrent/async_study/Cargo.lock generated Normal file
View File

@@ -0,0 +1,438 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "async_study"
version = "0.1.0"
dependencies = [
"bytes",
"futures",
"futures-core",
"futures-util",
"tokio",
"tokio-stream",
]
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bytes"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4872d67bab6358e59559027aa3b9157c53d9358c51423c17554809a8858e0f8"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "futures"
version = "0.3.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f73fe65f54d1e12b726f517d3e2135ca3125a437b6d998caf1962961f7172d9e"
dependencies = [
"futures-channel",
"futures-core",
"futures-executor",
"futures-io",
"futures-sink",
"futures-task",
"futures-util",
]
[[package]]
name = "futures-channel"
version = "0.3.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3083ce4b914124575708913bca19bfe887522d6e2e6d0952943f5eac4a74010"
dependencies = [
"futures-core",
"futures-sink",
]
[[package]]
name = "futures-core"
version = "0.3.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c09fd04b7e4073ac7156a9539b57a484a8ea920f79c7c675d05d289ab6110d3"
[[package]]
name = "futures-executor"
version = "0.3.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9420b90cfa29e327d0429f19be13e7ddb68fa1cccb09d65e5706b8c7a749b8a6"
dependencies = [
"futures-core",
"futures-task",
"futures-util",
]
[[package]]
name = "futures-io"
version = "0.3.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc4045962a5a5e935ee2fdedaa4e08284547402885ab326734432bed5d12966b"
[[package]]
name = "futures-macro"
version = "0.3.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33c1e13800337f4d4d7a316bf45a567dbcb6ffe087f16424852d97e97a91f512"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "futures-sink"
version = "0.3.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "21163e139fa306126e6eedaf49ecdb4588f939600f0b1e770f4205ee4b7fa868"
[[package]]
name = "futures-task"
version = "0.3.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57c66a976bf5909d801bbef33416c41372779507e7a6b3a5e25e4749c58f776a"
[[package]]
name = "futures-util"
version = "0.3.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d8b7abd5d659d9b90c8cba917f6ec750a74e2dc23902ef9cd4cc8c8b22e6036a"
dependencies = [
"futures-channel",
"futures-core",
"futures-io",
"futures-macro",
"futures-sink",
"futures-task",
"memchr",
"pin-project-lite",
"pin-utils",
"slab",
]
[[package]]
name = "hermit-abi"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
dependencies = [
"libc",
]
[[package]]
name = "libc"
version = "0.2.121"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "efaa7b300f3b5fe8eb6bf21ce3895e1751d9665086af2d64b42f19701015ff4f"
[[package]]
name = "lock_api"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "88943dd7ef4a2e5a4bfa2753aaab3013e34ce2533d1996fb18ef591e315e2b3b"
dependencies = [
"scopeguard",
]
[[package]]
name = "log"
version = "0.4.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710"
dependencies = [
"cfg-if",
]
[[package]]
name = "memchr"
version = "2.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a"
[[package]]
name = "mio"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "52da4364ffb0e4fe33a9841a98a3f3014fb964045ce4f7a45a398243c8d6b0c9"
dependencies = [
"libc",
"log",
"miow",
"ntapi",
"wasi",
"winapi",
]
[[package]]
name = "miow"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9f1c5b025cda876f66ef43a113f91ebc9f4ccef34843000e0adf6ebbab84e21"
dependencies = [
"winapi",
]
[[package]]
name = "ntapi"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c28774a7fd2fbb4f0babd8237ce554b73af68021b5f695a3cebd6c59bac0980f"
dependencies = [
"winapi",
]
[[package]]
name = "num_cpus"
version = "1.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19e64526ebdee182341572e50e9ad03965aa510cd94427a4549448f285e957a1"
dependencies = [
"hermit-abi",
"libc",
]
[[package]]
name = "once_cell"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87f3e037eac156d1775da914196f0f37741a274155e34a0b7e427c35d2a2ecb9"
[[package]]
name = "parking_lot"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87f5ec2493a61ac0506c0f4199f99070cbe83857b0337006a30f3e6719b8ef58"
dependencies = [
"lock_api",
"parking_lot_core",
]
[[package]]
name = "parking_lot_core"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "28141e0cc4143da2443301914478dc976a61ffdb3f043058310c70df2fed8954"
dependencies = [
"cfg-if",
"libc",
"redox_syscall",
"smallvec",
"windows-sys",
]
[[package]]
name = "pin-project-lite"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e280fbe77cc62c91527259e9442153f4688736748d24660126286329742b4c6c"
[[package]]
name = "pin-utils"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "proc-macro2"
version = "1.0.36"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029"
dependencies = [
"unicode-xid",
]
[[package]]
name = "quote"
version = "1.0.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4af2ec4714533fcdf07e886f17025ace8b997b9ce51204ee69b6da831c3da57"
dependencies = [
"proc-macro2",
]
[[package]]
name = "redox_syscall"
version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8380fe0152551244f0747b1bf41737e0f8a74f97a14ccefd1148187271634f3c"
dependencies = [
"bitflags",
]
[[package]]
name = "scopeguard"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "signal-hook-registry"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e51e73328dc4ac0c7ccbda3a494dfa03df1de2f46018127f60c693f2648455b0"
dependencies = [
"libc",
]
[[package]]
name = "slab"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9def91fd1e018fe007022791f865d0ccc9b3a0d5001e01aabb8b40e46000afb5"
[[package]]
name = "smallvec"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2dd574626839106c320a323308629dcb1acfc96e32a8cba364ddc61ac23ee83"
[[package]]
name = "socket2"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "66d72b759436ae32898a2af0a14218dbf55efde3feeb170eb623637db85ee1e0"
dependencies = [
"libc",
"winapi",
]
[[package]]
name = "syn"
version = "1.0.89"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea297be220d52398dcc07ce15a209fce436d361735ac1db700cab3b6cdfb9f54"
dependencies = [
"proc-macro2",
"quote",
"unicode-xid",
]
[[package]]
name = "tokio"
version = "1.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2af73ac49756f3f7c01172e34a23e5d0216f6c32333757c2c61feb2bbff5a5ee"
dependencies = [
"bytes",
"libc",
"memchr",
"mio",
"num_cpus",
"once_cell",
"parking_lot",
"pin-project-lite",
"signal-hook-registry",
"socket2",
"tokio-macros",
"winapi",
]
[[package]]
name = "tokio-macros"
version = "1.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b557f72f448c511a979e2564e55d74e6c4432fc96ff4f6241bc6bded342643b7"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "tokio-stream"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "50145484efff8818b5ccd256697f36863f587da82cf8b409c53adf1e840798e3"
dependencies = [
"futures-core",
"pin-project-lite",
"tokio",
]
[[package]]
name = "unicode-xid"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows-sys"
version = "0.32.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3df6e476185f92a12c072be4a189a0210dcdcf512a1891d6dff9edb874deadc6"
dependencies = [
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_msvc"
version = "0.32.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d8e92753b1c443191654ec532f14c199742964a061be25d77d7a96f09db20bf5"
[[package]]
name = "windows_i686_gnu"
version = "0.32.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a711c68811799e017b6038e0922cb27a5e2f43a2ddb609fe0b6f3eeda9de615"
[[package]]
name = "windows_i686_msvc"
version = "0.32.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "146c11bb1a02615db74680b32a68e2d61f553cc24c4eb5b4ca10311740e44172"
[[package]]
name = "windows_x86_64_gnu"
version = "0.32.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c912b12f7454c6620635bbff3450962753834be2a594819bd5e945af18ec64bc"
[[package]]
name = "windows_x86_64_msvc"
version = "0.32.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "504a2476202769977a040c6364301a3f65d0cc9e3fb08600b2bda150a0488316"

View File

@@ -0,0 +1,14 @@
[package]
name = "async_study"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
tokio = { version = "1.0", features = ["full"] }
futures-core = "0.3"
futures-util = { version = "0.3", default-features = false }
bytes = "1.1"
futures = "0.3"
tokio-stream = "0.1"

View File

@@ -0,0 +1,22 @@
Import `tokio`:
```
tokio = { version = "1.0", features = ["full"] }
```
Usage:
```rust
#[tokio::main]
async fn main() {
// TODO ...
}
```
`tokio` 提供以下几种 channels 类型:
* mpsc: 多生产者,单消费者 - https://docs.rs/tokio/1/tokio/sync/mpsc/index.html
* oneshot: 单生产者,单消费者 - https://docs.rs/tokio/1/tokio/sync/oneshot/index.html
* broadcast: 多生产者,多消费者 - https://docs.rs/tokio/1/tokio/sync/broadcast/index.html
* watch: 单生产者,多消费者 - https://docs.rs/tokio/1/tokio/sync/watch/index.html

View File

@@ -0,0 +1,8 @@
#[tokio::main]
async fn main() {
invoke().await
}
async fn invoke() {
println!("Hello World!");
}

View File

@@ -0,0 +1,23 @@
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
#[tokio::main]
async fn main() {
invoke().await
}
struct FutureImpl {}
impl Future for FutureImpl {
type Output = ();
fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
println!("Hello World!");
Poll::Ready(())
}
}
fn invoke() -> impl Future<Output=()> {
FutureImpl {}
}

View File

@@ -0,0 +1,35 @@
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
#[tokio::main]
async fn main() {
invoke().await
}
struct FutureImpl {
i: i32,
}
impl Future for FutureImpl {
type Output = ();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
if self.i == 0 {
let s = &mut *self;
s.i += 1;
println!("Hello World, pending");
cx.waker().wake_by_ref();
Poll::Pending
} else {
println!("Hello World, ready");
Poll::Ready(())
}
}
}
fn invoke() -> impl Future<Output=()> {
FutureImpl {
i: 0,
}
}

View File

@@ -0,0 +1,10 @@
fn main() {
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
invoke().await
});
}
async fn invoke() {
println!("Hello World!");
}

View File

@@ -0,0 +1,72 @@
use std::collections::VecDeque;
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::time::{Duration, Instant};
use futures::task;
// from: https://tokio.rs/tokio/tutorial/async
fn main() {
let mut mini_tokio = MiniTokio::new();
mini_tokio.spawn(async {
let when = Instant::now() + Duration::from_millis(10);
let future = Delay { when };
let out = future.await;
assert_eq!(out, "done");
});
mini_tokio.run();
}
struct MiniTokio {
tasks: VecDeque<Task>,
}
type Task = Pin<Box<dyn Future<Output=()> + Send>>;
impl MiniTokio {
fn new() -> MiniTokio {
MiniTokio {
tasks: VecDeque::new(),
}
}
/// Spawn a future onto the mini-tokio instance.
fn spawn<F>(&mut self, future: F) where F: Future<Output=()> + Send + 'static, {
self.tasks.push_back(Box::pin(future));
}
fn run(&mut self) {
let waker = task::noop_waker();
let mut cx = Context::from_waker(&waker);
while let Some(mut task) = self.tasks.pop_front() {
if task.as_mut().poll(&mut cx).is_pending() {
self.tasks.push_back(task);
}
}
}
}
struct Delay {
when: Instant,
}
impl Future for Delay {
type Output = &'static str;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<&'static str> {
if Instant::now() >= self.when {
println!("Hello world");
Poll::Ready("done")
} else {
// Ignore this line for now.
cx.waker().wake_by_ref();
Poll::Pending
}
}
}

View File

@@ -0,0 +1,24 @@
use std::time::Duration;
use tokio::sync::oneshot::channel;
#[tokio::main]
async fn main() {
let (tx1, rx1) = channel::<i32>();
let (_tx2, rx2) = channel::<i32>();
tokio::spawn(async move {
tokio::time::sleep(Duration::from_millis(200)).await;
tx1.send(1);
});
println!("start select");
tokio::select! {
val = rx1 => {
println!("rx1 completed first with {:?}", val);
}
val = rx2 => {
println!("rx2 completed first with {:?}", val);
}
}
println!("end select");
}

View File

@@ -0,0 +1,44 @@
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::time::Duration;
use tokio::sync::oneshot::{channel, Receiver};
#[tokio::main]
async fn main() {
let (tx1, rx1) = channel::<i32>();
let (_tx2, rx2) = channel::<i32>();
tokio::spawn(async move {
tokio::time::sleep(Duration::from_millis(200)).await;
let _ = tx1.send(1);
});
println!("start select");
MySelect { rx1, rx2 }.await;
println!("end select");
}
struct MySelect {
rx1: Receiver<i32>,
rx2: Receiver<i32>,
}
impl Future for MySelect {
type Output = ();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
if let Poll::Ready(val) = Pin::new(&mut self.rx1).poll(cx) {
println!("rx1 completed first with {:?}", val);
return Poll::Ready(());
}
if let Poll::Ready(val) = Pin::new(&mut self.rx2).poll(cx) {
println!("rx2 completed first with {:?}", val);
return Poll::Ready(());
}
Poll::Pending
}
}

View File

@@ -0,0 +1,40 @@
use std::time::Duration;
use tokio::{join, select};
use tokio::sync::oneshot::channel;
#[tokio::main]
async fn main() {
let (tx1, rx1) = channel::<i32>();
let a = tokio::spawn(async move {
tokio::time::sleep(Duration::from_millis(2000)).await;
let _ = tx1.send(1);
println!(">>> send tx1");
});
select! {
a = rx1 => {
println!("tx: {:?}", a);
}
b = run() => {
println!("run: {:?}", b);
}
c = tokio::time::sleep(Duration::from_millis(1500)) => {
println!("timeout: {:?}", c);
}
}
println!("join: {:?}", join!(a));
println!(">> pre end");
tokio::time::sleep(Duration::from_millis(2000)).await;
println!(">> end");
}
async fn run() {
for i in 0.. {
tokio::time::sleep(Duration::from_millis(300)).await;
println!("# {}", i);
}
}

View File

@@ -0,0 +1,98 @@
use std::future::Future;
use std::io::{self, ErrorKind};
use std::pin::Pin;
use std::task::{Context, Poll};
use std::time::Duration;
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, ReadBuf};
use tokio::sync::mpsc::{channel, Receiver, Sender};
use tokio::time::sleep;
#[tokio::main]
async fn main() {
let (sender, receiver) = channel::<StreamMessage>(512);
let stream = Stream { sender, receiver };
let (mut s, mut w) = tokio::io::split(stream);
let t = tokio::spawn(async move {
let mut buf = Vec::<u8>::with_capacity(1024);
loop {
match s.read_buf(&mut buf).await {
Ok(len) => println!("Len: {}, Buf: {:?}", len, &buf[..len]),
Err(e) => {
println!("Err: {:?}", e);
if e.to_string().contains("broken pipe") {
break;
}
}
}
}
});
sleep(Duration::from_secs(1)).await;
let _ = w.write_all(b"hello world").await;
sleep(Duration::from_secs(1)).await;
let _ = w.write_all(b"hello world").await;
sleep(Duration::from_secs(1)).await;
let _ = w.write_all(b"hello world").await;
let a = tokio::join!(w.shutdown());
let b = tokio::join!(t);
println!("{:?}, {:?}", a, b)
}
#[derive(Debug)]
enum StreamMessage {
Data(Vec<u8>),
Flush,
Close,
}
struct Stream {
sender: Sender<StreamMessage>,
receiver: Receiver<StreamMessage>,
}
impl AsyncRead for Stream {
fn poll_read(mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut ReadBuf<'_>) -> Poll<io::Result<()>> {
let m = self.receiver.poll_recv(cx);
match m {
Poll::Pending => Poll::Pending,
Poll::Ready(m_opt) => match m_opt {
None => Poll::Ready(Err(io::Error::new(ErrorKind::BrokenPipe, "broken pipe"))),
Some(StreamMessage::Close) => {
self.receiver.close();
Poll::Ready(Ok(()))
}
Some(StreamMessage::Flush) => Poll::Ready(Ok(())),
Some(StreamMessage::Data(mm)) => {
buf.put_slice(mm.as_slice());
Poll::Ready(Ok(()))
}
}
}
}
}
impl AsyncWrite for Stream {
fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<io::Result<usize>> {
pool_send(self, cx, StreamMessage::Data(Vec::from(buf)), buf.len())
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
pool_send(self, cx, StreamMessage::Flush, ())
}
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
pool_send(self, cx, StreamMessage::Close, ())
}
}
fn pool_send<R>(s: Pin<&mut Stream>, cx: &mut Context<'_>, stream_message: StreamMessage, r: R) -> Poll<io::Result<R>> {
let fut = s.sender.send(stream_message);
tokio::pin!( fut);
match fut.poll(cx) {
Poll::Pending => Poll::Pending,
Poll::Ready(Ok(_)) => Poll::Ready(Ok(r)),
Poll::Ready(Err(e)) => Poll::Ready(Err(io::Error::new(ErrorKind::BrokenPipe, e.to_string()))),
}
}

View File

@@ -0,0 +1,10 @@
use tokio_stream::{self as stream, StreamExt};
#[tokio::main]
async fn main() {
let mut stream = stream::iter(vec![0, 1, 2]);
while let Some(value) = stream.next().await {
println!("Got {}", value);
}
}

View File

@@ -0,0 +1,29 @@
use std::pin::Pin;
use std::task::{Context, Poll};
use futures_core::Stream;
use tokio_stream::StreamExt;
#[tokio::main]
async fn main() {
let mut stream = MyStream(0);
while let Some(value) = stream.next().await {
println!("Got {}", value);
}
}
struct MyStream(i32);
impl Stream for MyStream {
type Item = i32;
fn poll_next(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
if self.0 < 10 {
self.0 += 1;
Poll::Ready(Some(self.0))
} else {
Poll::Ready(None)
}
}
}

View File

@@ -0,0 +1,190 @@
use std::collections::HashMap;
use std::future::Future;
use std::io::{self, ErrorKind};
use std::pin::Pin;
use std::sync::{Arc, Mutex};
use std::sync::atomic::{AtomicU64, Ordering};
use std::task::{Context, Poll};
use std::time::Duration;
use bytes::BytesMut;
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, ReadBuf, ReadHalf, WriteHalf};
use tokio::sync::mpsc::{channel, Receiver, Sender};
use tokio::task::JoinHandle;
use tokio::time::sleep;
#[tokio::main]
async fn main() {
let channel_id = AtomicU64::new(1);
let (left_to_right_sender, left_to_right_receiver) = channel::<StreamPackage>(512);
let (right_to_left_sender, right_to_left_receiver) = channel::<StreamPackage>(512);
let right_consumer_stream_map = Arc::new(Mutex::new(HashMap::<u64, Sender<StreamPackage>>::new()));
let left_consumer_stream_map = Arc::new(Mutex::new(HashMap::<u64, Sender<StreamPackage>>::new()));
loop_consume(left_to_right_receiver, right_consumer_stream_map.clone());
loop_consume(right_to_left_receiver, left_consumer_stream_map.clone());
let mut handles = vec![];
for _ in 0..3 {
let new_channel_id = channel_id.fetch_add(1, Ordering::SeqCst);
let right_stream = create_stream(new_channel_id, right_to_left_sender.clone(), right_consumer_stream_map.clone());
let left_stream = create_stream(new_channel_id, left_to_right_sender.clone(), left_consumer_stream_map.clone());
let (right_stream_reader, right_stream_writer) = tokio::io::split(right_stream);
let (left_stream_reader, left_stream_writer) = tokio::io::split(left_stream);
let a = loop_read(right_stream_reader, format!("#{} right reader", new_channel_id));
let b = loop_read(left_stream_reader, format!("#{} left reader", new_channel_id));
let c = loop_send(new_channel_id, right_stream_writer, format!("right sender"));
let d = loop_send(new_channel_id, left_stream_writer, format!("left sender"));
handles.extend([a, b, c, d]);
}
for h in handles {
let _ = tokio::join!(h);
}
}
fn create_stream(channel_id: u64, sender: Sender<StreamPackage>, consumer_stream_map: Arc<Mutex<HashMap<u64, Sender<StreamPackage>>>>) -> Stream {
let (new_sender, receiver) = channel::<StreamPackage>(16);
{
let stream_map_left_locked = &mut consumer_stream_map.lock().unwrap();
stream_map_left_locked.insert(channel_id, new_sender);
}
Stream {
channel_id,
sender,
receiver,
}
}
fn loop_send(channel_id: u64, mut writer: WriteHalf<Stream>, tag: String) -> JoinHandle<()> {
tokio::spawn(async move {
for i in 0..3 {
sleep(Duration::from_millis(100)).await;
let _ = writer.write_all(format!("Send message: [{}] {} - {}", &tag, channel_id, i).as_bytes()).await;
}
let _ = writer.shutdown().await;
})
}
fn loop_read(mut reader: ReadHalf<Stream>, tag: String) -> JoinHandle<()> {
tokio::spawn(async move {
let mut buf = BytesMut::with_capacity(4096);
// let mut buf = Vec::with_capacity(4096);
loop {
buf.clear();
match reader.read_buf(&mut buf).await {
Ok(len) => println!("Tag: {}, Len: {}, Buf: [[ {} ]]", &tag, len, String::from_utf8_lossy(&buf[..len])),
Err(e) => {
println!("Tag: {}, Err: {:?}", &tag, e);
if e.to_string().contains("broken pipe") {
break;
}
}
}
}
})
}
fn loop_consume(mut receiver: Receiver<StreamPackage>, stream_map: Arc<Mutex<HashMap<u64, Sender<StreamPackage>>>>) {
tokio::spawn(async move {
loop {
match receiver.recv().await {
None => {
println!("Receiver none");
break;
}
Some(package) => {
let channel_id = package.channel_id;
let sender = {
let stream_map_locked = &stream_map.lock().unwrap();
stream_map_locked.get(&channel_id).map(|s| s.clone())
};
match sender {
None => println!("Channel id not found: {}", channel_id),
Some(sender) => {
// TODO process result
let _ = sender.send(package).await;
}
}
}
}
}
});
}
#[derive(Debug)]
struct StreamPackage {
channel_id: u64,
message: StreamPackageMessage,
}
#[derive(Debug)]
enum StreamPackageMessage {
Data(Vec<u8>),
Flush,
Close,
}
struct Stream {
channel_id: u64,
sender: Sender<StreamPackage>,
receiver: Receiver<StreamPackage>,
}
impl AsyncRead for Stream {
fn poll_read(mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut ReadBuf<'_>) -> Poll<io::Result<()>> {
let m = self.receiver.poll_recv(cx);
match m {
Poll::Pending => Poll::Pending,
Poll::Ready(m_opt) => match m_opt {
None => Poll::Ready(Err(io::Error::new(ErrorKind::BrokenPipe, "broken pipe"))),
Some(package) => match package.message {
StreamPackageMessage::Close => {
self.receiver.close();
// Poll::Ready(Ok(()))
Poll::Ready(Err(io::Error::new(ErrorKind::BrokenPipe, "broken pipe")))
}
StreamPackageMessage::Flush => Poll::Ready(Ok(())),
StreamPackageMessage::Data(mm) => {
buf.put_slice(mm.as_slice());
Poll::Ready(Ok(()))
}
}
}
}
}
}
impl AsyncWrite for Stream {
fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<io::Result<usize>> {
let stream_package = StreamPackage { channel_id: self.channel_id, message: StreamPackageMessage::Data(Vec::from(buf)) };
pool_send(self, cx, stream_package, buf.len())
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
let stream_package = StreamPackage { channel_id: self.channel_id, message: StreamPackageMessage::Flush };
pool_send(self, cx, stream_package, ())
}
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
let stream_package = StreamPackage { channel_id: self.channel_id, message: StreamPackageMessage::Close };
pool_send(self, cx, stream_package, ())
}
}
fn pool_send<R>(s: Pin<&mut Stream>, cx: &mut Context<'_>, stream_package: StreamPackage, r: R) -> Poll<io::Result<R>> {
let fut = s.sender.send(stream_package);
tokio::pin!( fut);
match fut.poll(cx) {
Poll::Pending => Poll::Pending,
Poll::Ready(Ok(_)) => Poll::Ready(Ok(r)),
Poll::Ready(Err(e)) => Poll::Ready(Err(io::Error::new(ErrorKind::BrokenPipe, e.to_string()))),
}
}

25
__concurrent/qcell/Cargo.lock generated Normal file
View File

@@ -0,0 +1,25 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "once_cell"
version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da32515d9f6e6e489d7bc9d84c71b060db7247dc035bbe44eac88cf87486d8d5"
[[package]]
name = "qcell"
version = "0.1.0"
dependencies = [
"qcell 0.5.0",
]
[[package]]
name = "qcell"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5023f06b72bfa9fbb5ebdbe891f02db9406ba026990e0778f80094bc84f3ac8"
dependencies = [
"once_cell",
]

View File

@@ -0,0 +1,10 @@
[package]
name = "qcell"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
qcell = "0.5.0"

View File

@@ -0,0 +1,16 @@
use std::rc::Rc;
use qcell::{QCell, QCellOwner};
// https://docs.rs/qcell/latest/qcell/
fn main() {
let mut owner = QCellOwner::new();
let item = Rc::new(QCell::new(&owner, Vec::<u8>::new()));
let iref = owner.rw(&item);
// test(&mut owner, &item); // Compile error
iref.push(1);
fn test(owner: &mut QCellOwner, item: &Rc<QCell<Vec<u8>>>) {
owner.rw(&item).push(2);
}
}

444
__concurrent/waitgroup-demo/Cargo.lock generated Normal file
View File

@@ -0,0 +1,444 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "addr2line"
version = "0.21.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a30b2e23b9e17a9f90641c7ab1549cd9b44f296d3ccbf309d2863cfe398a0cb"
dependencies = [
"gimli",
]
[[package]]
name = "adler"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "atomic-waker"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0"
[[package]]
name = "autocfg"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1fdabc7756949593fe60f30ec81974b613357de856987752631dea1e3394c80"
[[package]]
name = "backtrace"
version = "0.3.71"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26b05800d2e817c8b3b4b54abd461726265fa9789ae34330622f2db9ee696f9d"
dependencies = [
"addr2line",
"cc",
"cfg-if",
"libc",
"miniz_oxide",
"object",
"rustc-demangle",
]
[[package]]
name = "bitflags"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1"
[[package]]
name = "bytes"
version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9"
[[package]]
name = "cc"
version = "1.0.96"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "065a29261d53ba54260972629f9ca6bffa69bac13cd1fed61420f7fa68b9f8bd"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "gimli"
version = "0.28.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253"
[[package]]
name = "hermit-abi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024"
[[package]]
name = "libc"
version = "0.2.154"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae743338b92ff9146ce83992f766a31066a91a8c84a45e0e9f21e7cf6de6d346"
[[package]]
name = "lock_api"
version = "0.4.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17"
dependencies = [
"autocfg",
"scopeguard",
]
[[package]]
name = "memchr"
version = "2.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d"
[[package]]
name = "miniz_oxide"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d811f3e15f28568be3407c8e7fdb6514c1cda3cb30683f15b6a1a1dc4ea14a7"
dependencies = [
"adler",
]
[[package]]
name = "mio"
version = "0.8.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4a650543ca06a924e8b371db273b2756685faae30f8487da1b56505a8f78b0c"
dependencies = [
"libc",
"wasi",
"windows-sys 0.48.0",
]
[[package]]
name = "num_cpus"
version = "1.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43"
dependencies = [
"hermit-abi",
"libc",
]
[[package]]
name = "object"
version = "0.32.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6a622008b6e321afc04970976f62ee297fdbaa6f95318ca343e3eebb9648441"
dependencies = [
"memchr",
]
[[package]]
name = "parking_lot"
version = "0.12.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e4af0ca4f6caed20e900d564c242b8e5d4903fdacf31d3daf527b66fe6f42fb"
dependencies = [
"lock_api",
"parking_lot_core",
]
[[package]]
name = "parking_lot_core"
version = "0.9.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8"
dependencies = [
"cfg-if",
"libc",
"redox_syscall",
"smallvec",
"windows-targets 0.52.5",
]
[[package]]
name = "pin-project-lite"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02"
[[package]]
name = "proc-macro2"
version = "1.0.81"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d1597b0c024618f09a9c3b8655b7e430397a36d23fdafec26d6965e9eec3eba"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.36"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7"
dependencies = [
"proc-macro2",
]
[[package]]
name = "redox_syscall"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "469052894dcb553421e483e4209ee581a45100d31b4018de03e5a7ad86374a7e"
dependencies = [
"bitflags",
]
[[package]]
name = "rustc-demangle"
version = "0.1.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76"
[[package]]
name = "scopeguard"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
[[package]]
name = "signal-hook-registry"
version = "1.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a9e9e0b4211b72e7b8b6e85c807d36c212bdb33ea8587f7569562a84df5465b1"
dependencies = [
"libc",
]
[[package]]
name = "smallvec"
version = "1.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67"
[[package]]
name = "socket2"
version = "0.5.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce305eb0b4296696835b71df73eb912e0f1ffd2556a501fcede6e0c50349191c"
dependencies = [
"libc",
"windows-sys 0.52.0",
]
[[package]]
name = "syn"
version = "2.0.60"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "909518bc7b1c9b779f1bbf07f2929d35af9f0f37e47c6e9ef7f9dddc1e1821f3"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "tokio"
version = "1.37.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1adbebffeca75fcfd058afa480fb6c0b81e165a0323f9c9d39c9697e37c46787"
dependencies = [
"backtrace",
"bytes",
"libc",
"mio",
"num_cpus",
"parking_lot",
"pin-project-lite",
"signal-hook-registry",
"socket2",
"tokio-macros",
"windows-sys 0.48.0",
]
[[package]]
name = "tokio-macros"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "unicode-ident"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "waitgroup"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d1f50000a783467e6c0200f9d10642f4bc424e39efc1b770203e88b488f79292"
dependencies = [
"atomic-waker",
]
[[package]]
name = "waitgroup-demo"
version = "0.1.0"
dependencies = [
"tokio",
"waitgroup",
]
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "windows-sys"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
dependencies = [
"windows-targets 0.48.5",
]
[[package]]
name = "windows-sys"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
dependencies = [
"windows-targets 0.52.5",
]
[[package]]
name = "windows-targets"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
dependencies = [
"windows_aarch64_gnullvm 0.48.5",
"windows_aarch64_msvc 0.48.5",
"windows_i686_gnu 0.48.5",
"windows_i686_msvc 0.48.5",
"windows_x86_64_gnu 0.48.5",
"windows_x86_64_gnullvm 0.48.5",
"windows_x86_64_msvc 0.48.5",
]
[[package]]
name = "windows-targets"
version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f0713a46559409d202e70e28227288446bf7841d3211583a4b53e3f6d96e7eb"
dependencies = [
"windows_aarch64_gnullvm 0.52.5",
"windows_aarch64_msvc 0.52.5",
"windows_i686_gnu 0.52.5",
"windows_i686_gnullvm",
"windows_i686_msvc 0.52.5",
"windows_x86_64_gnu 0.52.5",
"windows_x86_64_gnullvm 0.52.5",
"windows_x86_64_msvc 0.52.5",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7088eed71e8b8dda258ecc8bac5fb1153c5cffaf2578fc8ff5d61e23578d3263"
[[package]]
name = "windows_aarch64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9985fd1504e250c615ca5f281c3f7a6da76213ebd5ccc9561496568a2752afb6"
[[package]]
name = "windows_i686_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
[[package]]
name = "windows_i686_gnu"
version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "88ba073cf16d5372720ec942a8ccbf61626074c6d4dd2e745299726ce8b89670"
[[package]]
name = "windows_i686_gnullvm"
version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87f4261229030a858f36b459e748ae97545d6f1ec60e5e0d6a3d32e0dc232ee9"
[[package]]
name = "windows_i686_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
[[package]]
name = "windows_i686_msvc"
version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db3c2bf3d13d5b658be73463284eaf12830ac9a26a90c717b7f771dfe97487bf"
[[package]]
name = "windows_x86_64_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4e4246f76bdeff09eb48875a0fd3e2af6aada79d409d33011886d3e1581517d9"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "852298e482cd67c356ddd9570386e2862b5673c85bd5f88df9ab6802b334c596"
[[package]]
name = "windows_x86_64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0"

View File

@@ -0,0 +1,10 @@
[package]
name = "waitgroup-demo"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
tokio = { version = "1.37.0", features = ["full"] }
waitgroup = "0.1.2"

View File

@@ -0,0 +1,26 @@
use std::time::Duration;
use tokio::task::yield_now;
use tokio::time;
use tokio::time::Instant;
use waitgroup::WaitGroup;
#[tokio::main]
async fn main() {
let wg = WaitGroup::new();
for i in 80..100 {
let w = wg.worker();
tokio::spawn(async move {
println!("Start work #{}", i);
time::sleep(Duration::from_millis(i * 100)).await;
println!("Finish work #{}", i);
drop(w);
});
yield_now().await;
}
let instant = Instant::now();
println!("Start waiting...");
wg.wait().await;
println!("Finish wait, elapsed: {}ms", instant.elapsed().as_millis());
}

4
__crypto/README.md Normal file
View File

@@ -0,0 +1,4 @@
Cryptography:
https://cryptography.rs/

454
__crypto/aegis-test/Cargo.lock generated Normal file
View File

@@ -0,0 +1,454 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "aead"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d122413f284cf2d62fb1b7db97e02edb8cda96d769b16e443a4f6195e35662b0"
dependencies = [
"crypto-common",
"generic-array",
]
[[package]]
name = "aegis"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f5bfadafb4f9094ee03767a64997e6645cf29243d6d4c04158212b8663f9d6d"
dependencies = [
"cc",
"softaes",
]
[[package]]
name = "aegis-test"
version = "0.1.0"
dependencies = [
"aegis",
"aes-gcm",
"ascon-aead",
"benchmark-simple",
"chacha20poly1305",
"rocca",
]
[[package]]
name = "aes"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac1f845298e95f983ff1944b728ae08b8cebab80d684f0a832ed0fc74dfa27e2"
dependencies = [
"cfg-if",
"cipher",
"cpufeatures",
]
[[package]]
name = "aes-gcm"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "209b47e8954a928e1d72e86eca7000ebb6655fe1436d33eefc2201cad027e237"
dependencies = [
"aead",
"aes",
"cipher",
"ctr",
"ghash",
"subtle",
]
[[package]]
name = "ascon"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9fe9a0cff241855e9166670d259192aacf1e9a81b865dc9905afbbab31a9d2c1"
[[package]]
name = "ascon-aead"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf081c54f55de655350e71e01dd36e093012f11447a3d575b907b84425887fa3"
dependencies = [
"aead",
"ascon",
"subtle",
"zeroize",
]
[[package]]
name = "benchmark-simple"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f87ada40e82bfa2716c35e99c1969299d3a17ce8ec05d58145d4056da16ea2c4"
dependencies = [
"precision",
]
[[package]]
name = "bumpalo"
version = "3.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a3e2c3daef883ecc1b5d58c15adae93470a91d425f3532ba1695849656af3fc1"
[[package]]
name = "cc"
version = "1.0.82"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "305fe645edc1442a0fa8b6726ba61d422798d37a52e12eaecf4b022ebbb88f01"
dependencies = [
"libc",
]
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chacha20"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3613f74bd2eac03dad61bd53dbe620703d4371614fe0bc3b9f04dd36fe4e818"
dependencies = [
"cfg-if",
"cipher",
"cpufeatures",
]
[[package]]
name = "chacha20poly1305"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "10cd79432192d1c0f4e1a0fef9527696cc039165d729fb41b3f4f4f354c2dc35"
dependencies = [
"aead",
"chacha20",
"cipher",
"poly1305",
"zeroize",
]
[[package]]
name = "cipher"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad"
dependencies = [
"crypto-common",
"inout",
"zeroize",
]
[[package]]
name = "cpufeatures"
version = "0.2.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a17b76ff3a4162b0b27f354a0c87015ddad39d35f9c0c36607a3bdd175dde1f1"
dependencies = [
"libc",
]
[[package]]
name = "crypto-common"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3"
dependencies = [
"generic-array",
"rand_core",
"typenum",
]
[[package]]
name = "ctr"
version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0369ee1ad671834580515889b80f2ea915f23b8be8d0daa4bbaf2ac5c7590835"
dependencies = [
"cipher",
]
[[package]]
name = "generic-array"
version = "0.14.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a"
dependencies = [
"typenum",
"version_check",
]
[[package]]
name = "getrandom"
version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be4136b2a15dd319360be1c07d9933517ccf0be8f16bf62a3bee4f0d618df427"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "ghash"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d930750de5717d2dd0b8c0d42c076c0e884c81a73e6cab859bbd2339c71e3e40"
dependencies = [
"opaque-debug",
"polyval",
]
[[package]]
name = "inout"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a0c10553d664a4d0bcff9f4215d0aac67a639cc68ef660840afe309b807bc9f5"
dependencies = [
"generic-array",
]
[[package]]
name = "libc"
version = "0.2.147"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3"
[[package]]
name = "log"
version = "0.4.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"
[[package]]
name = "once_cell"
version = "1.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d"
[[package]]
name = "opaque-debug"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5"
[[package]]
name = "poly1305"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8159bd90725d2df49889a078b54f4f79e87f1f8a8444194cdca81d38f5393abf"
dependencies = [
"cpufeatures",
"opaque-debug",
"universal-hash",
]
[[package]]
name = "polyval"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d52cff9d1d4dee5fe6d03729099f4a310a41179e0a10dbf542039873f2e826fb"
dependencies = [
"cfg-if",
"cpufeatures",
"opaque-debug",
"universal-hash",
]
[[package]]
name = "precision"
version = "0.1.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78ae7cc38dee6656eeb0eee56a8aef8aa40d5128acd9c4b8bdfb80ae08ee1993"
dependencies = [
"cc",
"libc",
"rustc_version",
"wasi",
"wasm-bindgen",
]
[[package]]
name = "proc-macro2"
version = "1.0.66"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae"
dependencies = [
"proc-macro2",
]
[[package]]
name = "rand_core"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom",
]
[[package]]
name = "rocca"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b52de49d9fde07d1241f2156a8958b65c71ce615f2ff8256ae5106513eb1713e"
dependencies = [
"aes",
]
[[package]]
name = "rustc_version"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366"
dependencies = [
"semver",
]
[[package]]
name = "semver"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0293b4b29daaf487284529cc2f5675b8e57c61f70167ba415a463651fd6a918"
[[package]]
name = "softaes"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff744c5d37b8ff11721ef14e92d1ec8743b5419d81513b3df22346cbee3d9d3e"
[[package]]
name = "subtle"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc"
[[package]]
name = "syn"
version = "2.0.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c324c494eba9d92503e6f1ef2e6df781e78f6a7705a0202d9801b198807d518a"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "typenum"
version = "1.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba"
[[package]]
name = "unicode-ident"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c"
[[package]]
name = "universal-hash"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc1de2c688dc15305988b563c3854064043356019f97a4b46276fe734c4f07ea"
dependencies = [
"crypto-common",
"subtle",
]
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "wasm-bindgen"
version = "0.2.87"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7706a72ab36d8cb1f80ffbf0e071533974a60d0a308d01a5d0375bf60499a342"
dependencies = [
"cfg-if",
"wasm-bindgen-macro",
]
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.87"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ef2b6d3c510e9625e5fe6f509ab07d66a760f0885d858736483c32ed7809abd"
dependencies = [
"bumpalo",
"log",
"once_cell",
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.87"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dee495e55982a3bd48105a7b947fd2a9b4a8ae3010041b9e0faab3f9cd028f1d"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
]
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.87"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b"
dependencies = [
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.87"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca6ad05a4870b2bf5fe995117d3728437bd27d7cd5f06f13c17443ef369775a1"
[[package]]
name = "zeroize"
version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a0956f1ba7c7909bfb66c2e9e4124ab6f6482560f6628b5aaeba39207c9aad9"
dependencies = [
"zeroize_derive",
]
[[package]]
name = "zeroize_derive"
version = "1.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69"
dependencies = [
"proc-macro2",
"quote",
"syn",
]

View File

@@ -0,0 +1,14 @@
[package]
name = "aegis-test"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
aegis = "0.4.4"
aes-gcm = "0.10.2"
ascon-aead = "0.4.2"
benchmark-simple = "0.1.8"
chacha20poly1305 = "0.10.1"
rocca = "0.3.0"

View File

@@ -0,0 +1,62 @@
Benchmark:
```shell
$ cargo r --release
aegis128l : 10.63 G/s
aes256-gcm : 546.31 M/s
aes128-gcm : 597.56 M/s
chacha20-poly1305 : 1.03 G/s
ascon128a : 330.85 M/s
rocca : 307.84 M/s
```
```shell
$ RUSTFLAGS="-C target-cpu=native" cargo r --release
aegis128l : 10.66 G/s
aes256-gcm : 701.21 M/s
aes128-gcm : 794.85 M/s
chacha20-poly1305 : 1.18 G/s
ascon128a : 347.18 M/s
rocca : 4.35 G/s
```
```shell
$ RUSTFLAGS="-Ctarget-feature=+aes,+sse4.1" cargo r --release
aegis128l : 10.68 G/s
aes256-gcm : 554.68 M/s
aes128-gcm : 608.58 M/s
chacha20-poly1305 : 1.04 G/s
ascon128a : 337.74 M/s
rocca : 2.75 G/s
```
```shell
$ RUSTFLAGS="-Ctarget-feature=+aes,+sse4.2" cargo r --release
aegis128l : 10.74 G/s
aes256-gcm : 706.54 M/s
aes128-gcm : 797.11 M/s
chacha20-poly1305 : 1.04 G/s
ascon128a : 338.19 M/s
rocca : 6.05 G/s
```
```shell
$ RUSTFLAGS="-C target-cpu=native -Ctarget-feature=+aes,+pclmulqdq,+sse4.1" cargo r --release
aegis128l : 10.62 G/s
aes256-gcm : 695.29 M/s
aes128-gcm : 778.97 M/s
chacha20-poly1305 : 1.17 G/s
ascon128a : 345.49 M/s
rocca : 4.28 G/s
```
```shell
$ RUSTFLAGS="-C target-cpu=native -Ctarget-feature=+aes,+pclmulqdq,+sse4.2" cargo r --release
aegis128l : 10.60 G/s
aes256-gcm : 692.76 M/s
aes128-gcm : 760.85 M/s
chacha20-poly1305 : 1.18 G/s
ascon128a : 344.81 M/s
rocca : 4.28 G/s
```

View File

@@ -0,0 +1,84 @@
use aegis::aegis128l::Aegis128L;
use aes_gcm::{
aead::{AeadInPlace as _, KeyInit as _},
Aes128Gcm, Aes256Gcm,
};
use benchmark_simple::*;
use chacha20poly1305::ChaCha20Poly1305;
use rocca::Rocca;
fn test_aes256gcm(m: &mut [u8]) {
let key = aes_gcm::Key::<Aes256Gcm>::from_slice(&[0u8; 32]);
let nonce = aes_gcm::Nonce::from_slice(&[0u8; 12]);
let state = Aes256Gcm::new(key);
state.encrypt_in_place_detached(nonce, &[], m).unwrap();
}
fn test_aes128gcm(m: &mut [u8]) {
let key = aes_gcm::Key::<Aes128Gcm>::from_slice(&[0u8; 16]);
let nonce = aes_gcm::Nonce::from_slice(&[0u8; 12]);
let state = Aes128Gcm::new(key);
state.encrypt_in_place_detached(nonce, &[], m).unwrap();
}
fn test_chacha20poly1305(m: &mut [u8]) {
let key = chacha20poly1305::Key::from_slice(&[0u8; 32]);
let nonce = chacha20poly1305::Nonce::from_slice(&[0u8; 12]);
let state = ChaCha20Poly1305::new(key);
state.encrypt_in_place_detached(nonce, &[], m).unwrap();
}
fn test_ascon128a(m: &mut [u8]) {
let key = [0u8; 16];
let nonce = [0u8; 16];
let state = ascon_aead::Ascon128a::new(key.as_slice().into());
state
.encrypt_in_place_detached(nonce.as_slice().into(), &[], m)
.unwrap();
}
fn test_aegis128l(m: &mut [u8]) {
let key = [0u8; 16];
let nonce = [0u8; 16];
let state = Aegis128L::<16>::new(&nonce, &key);
state.encrypt_in_place(m, &[]);
}
fn test_rocca(m: &mut [u8]) {
let key = [0u8; 32];
let nonce = [0u8; 16];
let state = Rocca::new(&nonce, &key);
state.encrypt_in_place(m, &[]);
}
fn main() {
let bench = Bench::new();
let mut m = vec![0xd0u8; 16384];
let options = &Options {
iterations: 10_000, // 100_000 -> 10_000 -> 1_000
warmup_iterations: 1_000,
min_samples: 5,
max_samples: 10,
max_rsd: 1.0,
..Default::default()
};
let res = bench.run(options, || test_aegis128l(&mut m));
println!("aegis128l : {}", res.throughput(m.len() as _));
let res = bench.run(options, || test_aes256gcm(&mut m));
println!("aes256-gcm : {}", res.throughput(m.len() as _));
let res = bench.run(options, || test_aes128gcm(&mut m));
println!("aes128-gcm : {}", res.throughput(m.len() as _));
let res = bench.run(options, || test_chacha20poly1305(&mut m));
println!("chacha20-poly1305 : {}", res.throughput(m.len() as _));
let res = bench.run(options, || test_ascon128a(&mut m));
println!("ascon128a : {}", res.throughput(m.len() as _));
let res = bench.run(options, || test_rocca(&mut m));
println!("rocca : {}", res.throughput(m.len() as _));
}

100
__crypto/aes_ctr_test/Cargo.lock generated Normal file
View File

@@ -0,0 +1,100 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "aes"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b169f7a6d4742236a0a00c541b845991d0ac43e546831af1249753ab4c3aa3a0"
dependencies = [
"cfg-if",
"cipher",
"cpufeatures",
]
[[package]]
name = "aes_ctr_test"
version = "0.1.0"
dependencies = [
"aes",
"hex",
]
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "cipher"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad"
dependencies = [
"crypto-common",
"inout",
]
[[package]]
name = "cpufeatures"
version = "0.2.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51e852e6dc9a5bed1fae92dd2375037bf2b768725bf3be87811edee3249d09ad"
dependencies = [
"libc",
]
[[package]]
name = "crypto-common"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3"
dependencies = [
"generic-array",
"typenum",
]
[[package]]
name = "generic-array"
version = "0.14.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a"
dependencies = [
"typenum",
"version_check",
]
[[package]]
name = "hex"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
[[package]]
name = "inout"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a0c10553d664a4d0bcff9f4215d0aac67a639cc68ef660840afe309b807bc9f5"
dependencies = [
"generic-array",
]
[[package]]
name = "libc"
version = "0.2.158"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d8adc4bb1803a324070e64a98ae98f38934d91957a99cfb3a43dcbc01bc56439"
[[package]]
name = "typenum"
version = "1.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825"
[[package]]
name = "version_check"
version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"

View File

@@ -0,0 +1,8 @@
[package]
name = "aes_ctr_test"
version = "0.1.0"
edition = "2021"
[dependencies]
aes = "0.8.4"
hex = "0.4.3"

View File

@@ -0,0 +1,42 @@
Test Java codes:
```java
public class AesCtrTest {
public static void main(String[] args) throws Exception {
final byte[] key = new byte[]{
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
};
final byte[] iv = new byte[]{
1, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0
};
final SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
final Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, keySpec, new IvParameterSpec(iv));
System.out.println(Bytes.from(cipher.doFinal("hello world".getBytes())).asHex());
System.out.println(Bytes.from(cipher.doFinal(
"hello world.hello world.hello world.hello world.hello world.hello world.".getBytes())).asHex());
}
}
```
Outputs:
```
84ad8d80732490c061177a
84ad8d80732490c061177a58bd26d032d6fcff2e66f9afe3cf95717485d3a4485d4a2a7bd835df3d0756b8192e3bf5a287ad8dd81942c43bc812c82d666ebbb34df4e2a5069467d9
```
<br>
Java `nonce` counter:
`com.sun.crypto.provider.CounterMode`
```java
private static void increment(byte[] nonce) {
for(int index = nonce.length - 1; index >= 0 && ++nonce[index] == 0; --index) {
}
}
```

View File

@@ -0,0 +1,109 @@
use aes::cipher::generic_array::GenericArray;
use aes::cipher::{Block, BlockEncryptMut, KeyInit};
use aes::Aes128;
fn main() {
let key = [0_u8; 16];
let iv = [0_u8; 16];
let plaintext = b"hello world";
let ciphertext = encrypt_ctr(&key, &iv, plaintext);
assert_eq!("0e8c27b880aa5b54fa209e", hex::encode(&ciphertext));
let plaintext = b"hello world.hello world.hello world.hello world.hello world.hello world.";
let ciphertext = encrypt_ctr(&key, &iv, plaintext);
assert_eq!("0e8c27b880aa5b54fa209e77a251474237c28ba18812544f5e1a713bcbc7323571e4bee008d3cf\
fe9c08b5d603de9a569ff0c6c7266b2e4c8591edd1fceead8c4f22664e3c1ff0f4", hex::encode(&ciphertext));
let key = [1_u8; 16];
let mut iv = [0_u8; 16];
for i in 0..8 {
iv[i] = i as u8 + 1;
}
let plaintext = b"hello world.hello world.hello world.hello world.hello world.hello world.";
let ciphertext = encrypt_ctr(&key, &iv, plaintext);
assert_eq!("84ad8d80732490c061177a58bd26d032d6fcff2e66f9afe3cf95717485d3a4485d4a2a7bd835df\
3d0756b8192e3bf5a287ad8dd81942c43bc812c82d666ebbb34df4e2a5069467d9", hex::encode(&ciphertext));
let key = [1_u8; 16];
let iv = [1, 2, 3, 4, 5, 6, 7, 0, 1, 1, 2, 2, 3, 3, 4, 4];
let plaintext = b"hello world.hello world.hello world.hello world.hello world.hello world.";
let ciphertext = encrypt_ctr(&key, &iv, plaintext);
assert_eq!("d373d6b3491c7b8940996f9efefc98d0f564d066a557cf0aa29935a638f99d077a85338e574ee6\
7f69da41b8a87569c324b4d421f1350e11d6be30721605b9dd6401bc3780da3452", hex::encode(&ciphertext));
let key = [1_u8; 16];
let iv = [1, 2, 3, 4, 5, 6, 7, 0, 1, 1, 2, 2, 3, 3, 4, 4];
let plaintext = b"hello world.hello world.hello world.hello world.hello world.h\
ello world.hello world.hello world.hello world.hello world.hello world.hello world.hello w\
orld.hello world.hello world.hello world.hello world.hello world.hello world.hello world.h\
ello world.hello world.hello world.hello world.hello world.hello world.hello world.hello w\
orld.hello world.hello world.hello world.hello world.hello world.hello world.hello world.h\
ello world.hello world.hello world.hello world.hello world.hello world.hello world.hello w\
orld.hello world.hello world.hello world.hello world.hello world.hello world.hello world.h\
ello world.hello world.hello world.hello world.hello world.hello world.hello world.hello w\
orld.hello world.hello world.hello world.hello world.hello world.hello world.hello world.h\
ello world.hello world.hello world.hello world.hello world.hello world.hello world.hello w\
orld.hello world.hello world.hello world.hello world.hello world.hello world.hello world.h\
ello world.hello world.hello world.hello world.";
let ciphertext = encrypt_ctr(&key, &iv, plaintext);
assert_eq!("d373d6b3491c7b8940996f9efefc98d0f564d066a557cf0aa29935a638f99d077a85338e574ee6\
7f69da41b8a87569c324b4d421f1350e11d6be30721605b9dd6401bc3780da3452033164403472b0ea84a7894b\
500100959ccc2cfbe2b6792691f1a1b047755c1961a730c7bcd941ab50b4ece7501d624d5af457875370dbc7e6\
0e73fd6ab10b5033fb3b3e2caed7489cc02bf174a3e7f04bd1e5c81c811314689b2bb7f6f0f0333d9b3bf637ff\
7ac34b32bc4905638204dbd97d4cefd1a823b5d49364c72d987807991eb703e47d56631f9f1a62c9f691bd3641\
509884144fc6e1b63e8177c8e1247ccc0769cb59cd973869f2ac3dd6cb90534ee42f69995e4af48de870936d21\
cf1b4ccd52e4bd0eafaff46dd6f34c91072ffd61f73050580b790deba9dc30d9557c4142a0dfa52b70e050a563\
abd69e87150da9325751a58bb3e162ea3a69347684e50710ea2bd25f6c285903d7b200a56a7eb042639410da17\
471c93e2b26b13091cc22ec81c3912a7121a1337a560d9733d904b6c6752993b2943861d3b770693281d3519fb\
69158df2353913aebe1e6c16787b8d2ff363c1c31bb8b491cec0214bd3ef61357569c8ecf9487087e22eca48b2\
d1c2c718ad7d3984eb7d0ca27b96653d90847ed138b581f8a01c0bea33c435e7f124f4009de059c46c006431fa\
5cf41ad9f16e6457389f928216ebd4efd048840a4dbc80da170c666a5293209866e9371f47005a12d6dc76f7fd\
57fc5b265cc38f4aef6e5496836d1c4c6d96b2c807997b3f61fd45f0acf505f3512ea04a0ad89cc35b344bdc3f\
2126b24bee40406ff00a733555316b8ccb57d661326d6b2d48a7a7fe33fbec23d69890b9368f6321d60b231cf2\
5680bcf7e8f43e443c19150b72394b5a9617fb8cc9666d43968fecdb56c22db6421ffbe3351fe479d9e7841288\
15fbeb7f1b5f9dc1f8b3d772741f7ef9884b5ca81d972d8537ea4c12cbf29c7e362ccd14ed83781db312da5d83\
8e11b20b050b6be472d8133f8f6525649dc9b967d40f430feb4cb3408a623a12658959f8cd8a115d8f63dd6e8b\
b8449937620b1cf783a0761fcbd4f48dcb572c1463a7c756bf9dd77d45f92cfe9a04dfb4d007a448d9b1a8c936\
9bcd35db598e7cdec566d075ff4c8d445e94ef49c0d0a78bb45921bf4c6e6b750a4d0f3310fc9a767a368ced10\
293987bf88026c69dcd805aea561d9b207c2710e6e90658ec5e0f8560449889ba8af852ea36b272aebb73ef59e\
74d1c0fe963ecbf1049743263681f5733cdf3b0b89b52667f63f65e4e7327ca45a2c1bd385f7c5fbf74b82e4f8\
0425c237813f2d39c4d7a06b49eec49b26dd234323747cf41eb4c2a7647b8548dcd72d59a4f0da2b1120759cfc\
dae5df80c55a9015fc48f985bda3d416f7301d72a4348953", hex::encode(&ciphertext));
}
fn encrypt_ctr(key: &[u8; 16], iv: &[u8; 16], plaintext: &[u8]) -> Vec<u8> {
let mut aes128 = Aes128::new_from_slice(key).unwrap();
let plaintext_len = plaintext.len();
let blocks_count = (plaintext_len / 16) + if plaintext_len % 16 == 0 { 0 } else { 1 };
let iv_u128 = u128::from_be_bytes(iv.clone());
let mut cipher_text = vec![0_u8; plaintext_len];
for i in 0..blocks_count {
let enc_iv_u128 = iv_u128 + i as u128;
let mut block: Block<Aes128> = GenericArray::from(enc_iv_u128.to_be_bytes()).clone();
aes128.encrypt_block_mut(&mut block);
let block_slice = block.as_slice();
for j in 0..block_slice.len() {
let oi = i * 16 + j;
if oi < plaintext_len {
cipher_text[oi] = block_slice[j] ^ plaintext[oi];
}
}
}
cipher_text
}
#[test]
fn test_aes() {
let mut aes128 = Aes128::new_from_slice(
&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
).unwrap();
let mut a: Block<Aes128> = GenericArray::from_slice(&[0_u8; 16]).clone();
aes128.encrypt_block_mut(&mut a);
println!("{}", hex::encode(a.as_slice()));
assert_eq!("66e94bd4ef8a2c3b884cfa59ca342b2e", hex::encode(a.as_slice()));
}

107
__crypto/aes_gcm_crypto/Cargo.lock generated Normal file
View File

@@ -0,0 +1,107 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "aes"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "433cfd6710c9986c576a25ca913c39d66a6474107b406f34f91d4a8923395241"
dependencies = [
"cfg-if",
"cipher",
"cpufeatures",
]
[[package]]
name = "aes_gcm_crypto"
version = "0.1.0"
dependencies = [
"aes",
"hex",
"hex-literal",
]
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "cipher"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d1873270f8f7942c191139cb8a40fd228da6c3fd2fc376d7e92d47aa14aeb59e"
dependencies = [
"crypto-common",
"inout",
]
[[package]]
name = "cpufeatures"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "28d997bd5e24a5928dd43e46dc529867e207907fe0b239c3477d924f7f2ca320"
dependencies = [
"libc",
]
[[package]]
name = "crypto-common"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3"
dependencies = [
"generic-array",
"typenum",
]
[[package]]
name = "generic-array"
version = "0.14.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bff49e947297f3312447abdca79f45f4738097cc82b06e72054d2223f601f1b9"
dependencies = [
"typenum",
"version_check",
]
[[package]]
name = "hex"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
[[package]]
name = "hex-literal"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ebdb29d2ea9ed0083cd8cece49bbd968021bd99b0849edb4a9a7ee0fdf6a4e0"
[[package]]
name = "inout"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a0c10553d664a4d0bcff9f4215d0aac67a639cc68ef660840afe309b807bc9f5"
dependencies = [
"generic-array",
]
[[package]]
name = "libc"
version = "0.2.139"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79"
[[package]]
name = "typenum"
version = "1.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba"
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"

View File

@@ -0,0 +1,11 @@
[package]
name = "aes_gcm_crypto"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
aes = "0.8.2"
hex = "0.4.3"
hex-literal = "0.3.4"

View File

@@ -0,0 +1,162 @@
use aes::Aes256;
use aes::cipher::{BlockEncrypt, Key, KeyInit};
use aes::cipher::generic_array::GenericArray;
use hex_literal::hex;
pub struct GCM {
aes: Aes256,
}
// R = 11100001 || 0(120)
const R: u128 = 0b11100001 << 120;
fn gmul_128(x: u128, y: u128) -> u128 {
let mut z = 0u128;
let mut v = y;
for i in (0..128).rev() {
let xi = (x >> i) & 1;
if xi != 0 {
z ^= v;
}
v = match v & 1 == 0 {
true => { v >> 1 }
false => { (v >> 1) ^ R }
};
}
z
}
fn u8to128(bytes: &[u8]) -> u128 {
let result = bytes.iter().rev().enumerate().fold(0, |acc, (i, &byte)| {
acc | (byte as u128) << (i * 8)
});
result
}
fn msb_s(s: usize, bytes: &[u8]) -> Vec<u8> {
let mut result = vec![];
let n = s / 8;
let remain = s % 8;
for i in 0..n {
result.push(bytes[i]);
}
if remain > 0 {
result.push(bytes[n] >> (8 - remain));
}
result
}
// incs(X)=MSBlen(X)-s(X) || [int(LSBs(X))+1 mod 2^s]s
fn inc_32(bits: u128) -> u128 {
let msb = bits >> 32;
let mut lsb = (bits & 0xffffffff) as u32;
lsb = lsb.wrapping_add(1);
msb << 32 | lsb as u128
}
fn ghash(key: u128, messages: &[u128]) -> u128 {
let mut y = 0u128;
for i in 0..messages.len() {
let yi = gmul_128(y ^ messages[i], key);
y = yi;
}
y
}
impl GCM {
pub fn new(key: [u8; 32]) -> Self {
let key = Key::<Aes256>::from_slice(&key);
let aes = Aes256::new(&key);
Self {
aes,
}
}
fn encrypt_block(&mut self, block: &[u8; 16]) -> Vec<u8> {
let mut block = GenericArray::from(block.clone());
self.aes.encrypt_block(&mut block);
block.to_vec()
}
fn ghash_key(&mut self) -> u128 {
u8to128(&self.encrypt_block(&[0u8; 16]))
}
pub fn gctr(&mut self, iv: u128, message: &[u8]) -> Vec<u8> {
// 如果X是空串, 则直接返回
if message.len() == 0 {
return vec![];
}
let mut output = vec![];
let mut cb = iv;
for chunk in message.chunks(16) {
if chunk.len() < 16 {
let msb = msb_s(chunk.len() * 8, &self.encrypt_block(&cb.to_be_bytes()));
let y = u8to128(chunk) ^ u8to128(&msb);
output.extend_from_slice(&y.to_be_bytes()[16 - chunk.len()..16]);
} else {
let y = u8to128(chunk) ^ u8to128(&self.encrypt_block(&cb.to_be_bytes()));
output.extend_from_slice(&y.to_be_bytes());
}
// counter + 1
cb = inc_32(cb);
}
output
}
pub fn ae(&mut self, iv_bytes: &[u8], adata: &[u8], message: &[u8]) -> (Vec<u8>, Vec<u8>) {
let ghash_key = self.ghash_key();
let mut iv_padding = vec![];
let iv = u8to128(iv_bytes);
let j0 = match iv_bytes.len() == 12 {
true => {
iv << 32 | 0x00000001
}
false => {
// s = 128[len(iv) / 128] - len(iv)
let s = 128 * (((iv_bytes.len() * 8) + 128 - 1) / 128) - (iv_bytes.len() * 8);
iv_padding.push(iv << s);
iv_padding.push((iv_bytes.len() * 8) as u128);
ghash(ghash_key, &iv_padding)
}
};
let message_len = message.len() * 8;
let adata_len = adata.len() * 8;
let u = 128 * ((message_len + 128 - 1) / 128) - message_len;
let v = 128 * ((adata_len + 128 - 1) / 128) - adata_len;
// println!("u, v: {}, {}", u, v);
// println!("j0 = {:02x?}", j0);
let enc = self.gctr(inc_32(j0), &message);
let mut bit_string = Vec::<u8>::new();
bit_string.extend_from_slice(adata);
bit_string.extend_from_slice(&vec![0x00; v / 8]);
// 这里认证的是密文
bit_string.extend_from_slice(&enc);
bit_string.extend_from_slice(&vec![0x00; u / 8]);
bit_string.extend_from_slice(&(adata_len as u64).to_be_bytes());
bit_string.extend_from_slice(&(message_len as u64).to_be_bytes());
// println!("len = {}, bit_string[u8] = {:02x?}", bit_string.len(), bit_string);
let bit_string: Vec<u128> = bit_string.chunks(16).map(|it| u8to128(it)).collect();
// println!("bit_string[u128] = {:02x?}", bit_string);
let s = ghash(ghash_key, &bit_string).to_be_bytes();
// println!("{:02x?}", s);
let tag = self.gctr(j0, &s);
// println!("tag = {:02x?}", tag);
// println!("enc = {:02x?}", enc);
(tag, enc)
}
}
// https://developer.aliyun.com/article/952809
fn main() {
let key: [u8; 32] = hex!("0001020304050607080910111213141516171819202122232425262728293031");
let p: Vec<u8> = "Hello world!".as_bytes().to_vec();
let iv = hex!("000102030405060708091011");
let mut gcm = GCM::new(key);
// let h = gcm.ghash_key();
// assert_eq!(h, u8to128(&hex!("66e94bd4ef8a2c3b884cfa59ca342b2e")));
let (tag, enc) = gcm.ae(&iv, &[], &p);
println!("{}{}", hex::encode(&enc), hex::encode(tag));
println!("dce9511866417cff5123fa08c9e92cf156c5fc8bf6108ff28816fb58");
}

185
__crypto/asn1-der-test/Cargo.lock generated Normal file
View File

@@ -0,0 +1,185 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "asn1-der-test"
version = "0.1.0"
dependencies = [
"asn1-rs",
"base64",
"hex",
]
[[package]]
name = "asn1-rs"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22ad1373757efa0f70ec53939aabc7152e1591cb485208052993070ac8d2429d"
dependencies = [
"asn1-rs-derive",
"asn1-rs-impl",
"displaydoc",
"nom",
"num-traits",
"rusticata-macros",
"thiserror",
]
[[package]]
name = "asn1-rs-derive"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7378575ff571966e99a744addeff0bff98b8ada0dedf1956d59e634db95eaac1"
dependencies = [
"proc-macro2",
"quote",
"syn",
"synstructure",
]
[[package]]
name = "asn1-rs-impl"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b18050c2cd6fe86c3a76584ef5e0baf286d038cda203eb6223df2cc413565f7"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "autocfg"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1fdabc7756949593fe60f30ec81974b613357de856987752631dea1e3394c80"
[[package]]
name = "base64"
version = "0.22.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9475866fec1451be56a3c2400fd081ff546538961565ccb5b7142cbd22bc7a51"
[[package]]
name = "displaydoc"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "487585f4d0c6655fe74905e2504d8ad6908e4db67f744eb140876906c2f3175d"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "hex"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
[[package]]
name = "memchr"
version = "2.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149"
[[package]]
name = "minimal-lexical"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
[[package]]
name = "nom"
version = "7.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a"
dependencies = [
"memchr",
"minimal-lexical",
]
[[package]]
name = "num-traits"
version = "0.2.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da0df0e5185db44f69b44f26786fe401b6c293d1907744beaa7fa62b2e5a517a"
dependencies = [
"autocfg",
]
[[package]]
name = "proc-macro2"
version = "1.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e835ff2298f5721608eb1a980ecaee1aef2c132bf95ecc026a11b7bf3c01c02e"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef"
dependencies = [
"proc-macro2",
]
[[package]]
name = "rusticata-macros"
version = "4.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "faf0c4a6ece9950b9abdb62b1cfcf2a68b3b67a10ba445b3bb85be2a293d0632"
dependencies = [
"nom",
]
[[package]]
name = "syn"
version = "2.0.55"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "002a1b3dbf967edfafc32655d0f377ab0bb7b994aa1d32c8cc7e9b8bf3ebb8f0"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "synstructure"
version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c8af7666ab7b6390ab78131fb5b0fce11d6b7a6951602017c35fa82800708971"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "thiserror"
version = "1.0.58"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "03468839009160513471e86a034bb2c5c0e4baae3b43f79ffc55c4a5427b3297"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.58"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c61f3ba182994efc43764a46c018c347bc492c79f024e705f46567b418f6d4f7"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "unicode-ident"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"

View File

@@ -0,0 +1,11 @@
[package]
name = "asn1-der-test"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
asn1-rs = "0.6.1"
base64 = "0.22.0"
hex = "0.4.3"

View File

@@ -0,0 +1,91 @@
use std::str::FromStr;
use asn1_rs::{
BitString, Class, Error, Explicit, Integer,
OctetString, Oid, Sequence, TaggedValue, ToDer,
};
use base64::{Engine, engine::GeneralPurpose};
const BASE64: GeneralPurpose = base64::engine::general_purpose::STANDARD;
// -----BEGIN PRIVATE KEY-----
// MIG2AgEAMBAGByqGSM49AgEGBSuBBAAiBIGeMIGbAgEBBDB0n4af3g3zaB67WD+/
// Xe+FNOkwvuoDd/YUPh0y5ZfoBlkqG1WGNkZN2/d4EYqrNrahZANiAAR2vgKLjiAd
// I6l4EAyxGKwGBJ6pAdYZvNlBi8X9kKlCj6aYi5kSKqUfAMaQAX9+0WPoQ6Cy/gzi
// bPPmlPRMa4SdTKgZvvw5nuyNXZQU/wN69Opz/hCqxZHKYx5zSznj4/g=
// -----END PRIVATE KEY-----
// 0 182: SEQUENCE {
// 3 1: INTEGER 0
// 6 16: SEQUENCE {
// 8 7: OBJECT IDENTIFIER ecPublicKey (1 2 840 10045 2 1)
// 17 5: OBJECT IDENTIFIER secp384r1 (1 3 132 0 34)
// : }
// 24 158: OCTET STRING, encapsulates {
// 27 155: SEQUENCE {
// 30 1: INTEGER 1
// 33 48: OCTET STRING
// : 74 9F 86 9F DE 0D F3 68 1E BB 58 3F BF 5D EF 85
// : 34 E9 30 BE EA 03 77 F6 14 3E 1D 32 E5 97 E8 06
// : 59 2A 1B 55 86 36 46 4D DB F7 78 11 8A AB 36 B6
// 83 100: [1] {
// 85 98: BIT STRING
// : 04 76 BE 02 8B 8E 20 1D 23 A9 78 10 0C B1 18 AC
// : 06 04 9E A9 01 D6 19 BC D9 41 8B C5 FD 90 A9 42
// : 8F A6 98 8B 99 12 2A A5 1F 00 C6 90 01 7F 7E D1
// : 63 E8 43 A0 B2 FE 0C E2 6C F3 E6 94 F4 4C 6B 84
// : 9D 4C A8 19 BE FC 39 9E EC 8D 5D 94 14 FF 03 7A
// : F4 EA 73 FE 10 AA C5 91 CA 63 1E 73 4B 39 E3 E3
// : F8
// : }
// : }
// : }
// : }
const PRIVATE_KEY: &str = "749F869FDE0DF3681EBB583FBF5DEF8534E930BEEA0377F6143E1D32E597E806592A1B558636464DDBF778118AAB36B6";
const PUBLIC_KEY: &str = "0476BE028B8E201D23A978100CB118AC06049EA901D619BCD9418BC5FD90A9428FA6988B99122AA51F00C690017F7ED16\
3E843A0B2FE0CE26CF3E694F44C6B849D4CA819BEFC399EEC8D5D9414FF037AF4EA73FE10AAC591CA631E734B39E3E3F8";
fn main() {
let ec_public_key = Oid::from_str("1.2.840.10045.2.1").unwrap();
let secp_384_r1 = Oid::from_str("1.3.132.0.34").unwrap();
let private_key_oid_seq = {
let mut private_key_oid = Vec::new();
ec_public_key.write_der(&mut private_key_oid).unwrap();
secp_384_r1.write_der(&mut private_key_oid).unwrap();
Sequence::new(private_key_oid.into())
};
let private_body_seq = {
let private_key = hex::decode(PRIVATE_KEY).unwrap();
let public_key = hex::decode(PUBLIC_KEY).unwrap();
let integer_1 = Integer::from_i8(1);
let private_key_oct = OctetString::new(&private_key);
let mut private_body = Vec::new();
integer_1.write_der(&mut private_body).unwrap();
private_key_oct.write_der(&mut private_body).unwrap();
type TaggedValueContextSpecific<'a> = TaggedValue<BitString<'a>, Error, Explicit, { Class::CONTEXT_SPECIFIC }, 1>;
let public_key_bit = BitString::new(0, &public_key);
let public_key_tag = TaggedValueContextSpecific::explicit(public_key_bit);
public_key_tag.write_der(&mut private_body).unwrap();
Sequence::new(private_body.into())
};
let private_body = private_body_seq.to_der_vec().unwrap();
let private_body_oct = OctetString::new(&private_body);
let private_key_info_seq = {
let mut private_key_info = Vec::new();
let integer_0 = Integer::from_i8(0);
integer_0.write_der(&mut private_key_info).unwrap();
private_key_oid_seq.write_der(&mut private_key_info).unwrap();
private_body_oct.write_der(&mut private_key_info).unwrap();
Sequence::new(private_key_info.into())
};
println!("BASED64:\n{}", BASE64.encode(private_key_info_seq.to_der_vec().unwrap()));
println!("HEX:\n{}", hex::encode(private_key_info_seq.to_der_vec().unwrap()));
}

View File

@@ -0,0 +1,52 @@
use asn1_rs::{BitString, Class, Error, Explicit, Integer, OctetString, Oid, ParseResult, Sequence, TaggedValue};
use asn1_rs::FromDer;
use base64::Engine;
use base64::engine::GeneralPurpose;
const BASE64: GeneralPurpose = base64::engine::general_purpose::STANDARD;
fn main() {
let private_key = "MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgakRmuDPA8pr1kqmY\
PxjoMJSJzMeWllw0qbKWzrbEfzehRANCAAQs2XCluo7NF7Y0tL6Pp2GWz2fw5llF\
dRvIpq0j8kVQjNOIYZqlBG6OOMQql3U95HsJJY0YzkoXEcdcJmoIV0zn";
let private_key_der = BASE64.decode(private_key).unwrap();
let r: ParseResult<()> = Sequence::from_der_and_then(&private_key_der, |i| {
let (i, i0) = Integer::from_der(i)?;
println!("INTEGER0: {}", i0.as_i8().unwrap());
let r: ParseResult<()> = Sequence::from_der_and_then(i, |i| {
let (i, oid1) = Oid::from_der(i)?;
let (i, oid2) = Oid::from_der(i)?;
println!("OID1: {}", oid1);
println!("OID2: {}", oid2);
Ok((i, ()))
});
let (i, _) = r.unwrap();
let (i, octet) = OctetString::from_der(i).unwrap();
let octet = octet.as_ref();
let r: ParseResult<()> = Sequence::from_der_and_then(octet, |i| {
let (i, i1) = Integer::from_der(i).unwrap();
println!("INTEGER1: {}", i1.as_i8().unwrap());
let (i, oct) = OctetString::from_der(i)?;
println!("PRIVATE KEY: {}", hex::encode(oct.as_ref()));
type TaggedValueContextSpecific<'a> = TaggedValue<BitString<'a>, Error, Explicit, { Class::CONTEXT_SPECIFIC }, 1>;
let tagged_value = TaggedValueContextSpecific::from_der(i);
let _ = tagged_value.map(|(_i, tagged_value)| {
println!("Tagged value [{}]", tagged_value.tag().0);
let tagged_value_inner = tagged_value.into_inner();
println!("Tagged value: {}, unused: {}", hex::encode(tagged_value_inner.as_ref()), tagged_value_inner.unused_bits);
});
Ok((i, ()))
});
r.unwrap();
Ok((i, ()))
});
r.unwrap();
}

284
__crypto/bc1-btc-address/Cargo.lock generated Normal file
View File

@@ -0,0 +1,284 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "arrayvec"
version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50"
[[package]]
name = "base58ck"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c8d66485a3a2ea485c1913c4572ce0256067a5377ac8c75c4960e1cda98605f"
dependencies = [
"bitcoin-internals",
"bitcoin_hashes",
]
[[package]]
name = "bc1-btc-address"
version = "0.1.0"
dependencies = [
"bitcoin",
"hex",
"rand",
]
[[package]]
name = "bech32"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d965446196e3b7decd44aa7ee49e31d630118f90ef12f97900f262eb915c951d"
[[package]]
name = "bitcoin"
version = "0.32.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ad8929a18b8e33ea6b3c09297b687baaa71fb1b97353243a3f1029fad5c59c5b"
dependencies = [
"base58ck",
"bech32",
"bitcoin-internals",
"bitcoin-io",
"bitcoin-units",
"bitcoin_hashes",
"hex-conservative",
"hex_lit",
"secp256k1",
]
[[package]]
name = "bitcoin-internals"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30bdbe14aa07b06e6cfeffc529a1f099e5fbe249524f8125358604df99a4bed2"
[[package]]
name = "bitcoin-io"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b47c4ab7a93edb0c7198c5535ed9b52b63095f4e9b45279c6736cec4b856baf"
[[package]]
name = "bitcoin-units"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5285c8bcaa25876d07f37e3d30c303f2609179716e11d688f51e8f1fe70063e2"
dependencies = [
"bitcoin-internals",
]
[[package]]
name = "bitcoin_hashes"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb18c03d0db0247e147a21a6faafd5a7eb851c743db062de72018b6b7e8e4d16"
dependencies = [
"bitcoin-io",
"hex-conservative",
]
[[package]]
name = "bitflags"
version = "2.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967"
[[package]]
name = "cc"
version = "1.2.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d487aa071b5f64da6f19a3e848e3578944b726ee5a4854b82172f02aa876bfdc"
dependencies = [
"shlex",
]
[[package]]
name = "cfg-if"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9555578bc9e57714c812a1f84e4fc5b4d21fcb063490c624de019f7464c91268"
[[package]]
name = "getrandom"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26145e563e54f2cadc477553f1ec5ee650b00862f0a58bcd12cbdc5f0ea2d2f4"
dependencies = [
"cfg-if",
"libc",
"r-efi",
"wasi",
]
[[package]]
name = "hex"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
[[package]]
name = "hex-conservative"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5313b072ce3c597065a808dbf612c4c8e8590bdbf8b579508bf7a762c5eae6cd"
dependencies = [
"arrayvec",
]
[[package]]
name = "hex_lit"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3011d1213f159867b13cfd6ac92d2cd5f1345762c63be3554e84092d85a50bbd"
[[package]]
name = "libc"
version = "0.2.174"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1171693293099992e19cddea4e8b849964e9846f4acee11b3948bcc337be8776"
[[package]]
name = "ppv-lite86"
version = "0.2.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9"
dependencies = [
"zerocopy",
]
[[package]]
name = "proc-macro2"
version = "1.0.95"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d"
dependencies = [
"proc-macro2",
]
[[package]]
name = "r-efi"
version = "5.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f"
[[package]]
name = "rand"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9fbfd9d094a40bf3ae768db9361049ace4c0e04a4fd6b359518bd7b73a73dd97"
dependencies = [
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99d9a13982dcf210057a8a78572b2217b667c3beacbf3a0d8b454f6f82837d38"
dependencies = [
"getrandom",
]
[[package]]
name = "secp256k1"
version = "0.29.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9465315bc9d4566e1724f0fffcbcc446268cb522e60f9a27bcded6b19c108113"
dependencies = [
"bitcoin_hashes",
"secp256k1-sys",
]
[[package]]
name = "secp256k1-sys"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d4387882333d3aa8cb20530a17c69a3752e97837832f34f6dccc760e715001d9"
dependencies = [
"cc",
]
[[package]]
name = "shlex"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]]
name = "syn"
version = "2.0.104"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17b6f705963418cdb9927482fa304bc562ece2fdd4f616084c50b7023b435a40"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"
[[package]]
name = "wasi"
version = "0.14.2+wasi-0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9683f9a5a998d873c0d21fcbe3c083009670149a8fab228644b8bd36b2c48cb3"
dependencies = [
"wit-bindgen-rt",
]
[[package]]
name = "wit-bindgen-rt"
version = "0.39.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1"
dependencies = [
"bitflags",
]
[[package]]
name = "zerocopy"
version = "0.8.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1039dd0d3c310cf05de012d8a39ff557cb0d23087fd44cad61df08fc31907a2f"
dependencies = [
"zerocopy-derive",
]
[[package]]
name = "zerocopy-derive"
version = "0.8.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ecf5b4cc5364572d7f4c329661bcc82724222973f2cab6f050a4e5c22f75181"
dependencies = [
"proc-macro2",
"quote",
"syn",
]

View File

@@ -0,0 +1,9 @@
[package]
name = "bc1-btc-address"
version = "0.1.0"
edition = "2024"
[dependencies]
bitcoin = "0.32.6"
hex = "0.4.3"
rand = "0.9.1"

View File

@@ -0,0 +1,29 @@
use bitcoin::key::Secp256k1;
use bitcoin::{Address, CompressedPublicKey, Network, PrivateKey};
use rand::RngCore;
// generate btc address:
// https://coinb.in/#newSegWit
fn main() {
let secp = Secp256k1::new();
let mut sk_bytes = [0u8; 32];
rand::rng().fill_bytes(&mut sk_bytes);
let secret_key = PrivateKey::from_slice(&sk_bytes, Network::Bitcoin).unwrap();
// let secret_key = PrivateKey::from_wif("L2HajtedHgDT16o3Er8L8tyRrPNbXkeBK69vhGi7K9eK4Yg3dJuj").unwrap();
let public_key = secret_key.public_key(&secp);
let compress_public_key = CompressedPublicKey::try_from(public_key).unwrap();
// 构造 P2WPKH 地址 (bc1 开头)
let address = Address::p2wpkh(&compress_public_key, Network::Bitcoin);
let address = address.to_string();
println!("Private Key (WIF): {}", secret_key.to_wif());
println!("Public Key: {}", public_key);
println!(
"Public Key: {}",
hex::encode(&public_key.inner.serialize_uncompressed())
);
println!("SegWit Address (bc1): {}", address);
}

View File

@@ -1,26 +1,53 @@
use std::{ thread, sync::{ Arc, Mutex } }; use std::{sync::{Arc, Mutex}, thread};
use rand::rngs::OsRng;
use secp256k1::{ Secp256k1, key::PublicKey };
use sha2::Sha256;
use ripemd160::Ripemd160;
use digest::{ Input, FixedOutput };
const PREFIX: &[&str] = &["1Hatter"]; use digest::{FixedOutput, Input};
use rand::rngs::OsRng;
use ripemd160::Ripemd160;
use secp256k1::{key::PublicKey, Secp256k1};
use sha2::Sha256;
fn main() { fn main() {
println!("Prefix: {:?}", PREFIX); let args = std::env::args();
if args.len() <= 1 {
println!("[ERROR] Need at least one argument, e.g. 1Hatter 1UTF8 ...");
return;
}
let continue_on_found = std::env::var("CONTINUE_ON_FOUND")
.map(|c| c.to_lowercase())
.map(|c| c == "1" || c == "true" || c == "yes" || c == "on")
.unwrap_or_else(|_| false);
let prefixes: Vec<String> = args.skip(1).collect();
println!("Prefixes: {:?}", prefixes);
if continue_on_found {
println!("CONTINUE_ON_FOUND is ON");
} else {
println!("CONTINUE_ON_FOUND is OFF");
}
let loop_count = std::env::var("LOOP_COUNT")
.map(|c| c.parse::<u64>())
.unwrap_or_else(|_| Ok(10_000_000_u64))
.unwrap_or_else(|_| 10_000_000_u64);
println!("LOOP_COUNT={}", loop_count);
let num_of_vcpus = num_cpus::get(); let num_of_vcpus = num_cpus::get();
let num_of_phycpus = num_cpus::get_physical(); let num_of_phycpus = num_cpus::get_physical();
println!("You have {} vCPUs, from {} phyCPUs", num_of_vcpus, num_of_phycpus); println!("You have {} vCPUs, from {} phyCPUs", num_of_vcpus, num_of_phycpus);
let concurrent_count = std::env::var("CONCURRENT_COUNT")
.map(|c| c.parse::<u64>())
.unwrap_or_else(|_| Ok(num_of_phycpus as u64))
.unwrap_or_else(|_| 1_u64);
println!("CONCURRENT_COUNT={}", concurrent_count);
let stop_flag = Arc::new(Mutex::new(false)); let stop_flag = Arc::new(Mutex::new(false));
let mut handles = vec![]; let mut handles = vec![];
for ind in 0..num_of_phycpus { for ind in 0..concurrent_count {
println!("- Running task {} of {}", ind, num_of_phycpus); println!("- Running task {} of {}", ind, concurrent_count);
let the_stop_flag = Arc::clone(&stop_flag); let the_stop_flag = Arc::clone(&stop_flag);
let prefixes = prefixes.clone();
let child = thread::spawn(move || { let child = thread::spawn(move || {
run_one_task(ind, the_stop_flag); run_one_task(prefixes, continue_on_found, loop_count, ind, the_stop_flag);
}); });
handles.push(child); handles.push(child);
} }
@@ -30,11 +57,11 @@ fn main() {
println!("Finished!"); println!("Finished!");
} }
fn run_one_task(ind: usize, the_stop_flag: Arc<Mutex<bool>>) { fn run_one_task(prefixes: Vec<String>, continue_on_found: bool, loop_count: u64, ind: u64, the_stop_flag: Arc<Mutex<bool>>) {
let secp = Secp256k1::new(); let secp = Secp256k1::new();
let mut rng = OsRng::new().expect("OsRng"); let mut rng = OsRng::new().expect("OsRng");
for i in 0..10_000_000 { for i in 0..loop_count {
let (secret_key, public_key) = secp.generate_keypair(&mut rng); let (secret_key, public_key) = secp.generate_keypair(&mut rng);
let s = make_btc_address(&public_key); let s = make_btc_address(&public_key);
@@ -47,12 +74,13 @@ fn run_one_task(ind: usize, the_stop_flag: Arc<Mutex<bool>>) {
println!("> {} - {}", ind, i); println!("> {} - {}", ind, i);
} }
} }
if PREFIX.iter().any(|p| s.starts_with(p)) { if prefixes.iter().any(|p| s.starts_with(p)) {
println!("{}", s); println!(">> {}\n{}", s, secret_key);
println!("{}", secret_key);
*the_stop_flag.lock().unwrap() = true; if !continue_on_found {
break; *the_stop_flag.lock().unwrap() = true;
break;
}
} }
} }
} }

31
__crypto/crypto2-demo/Cargo.lock generated Normal file
View File

@@ -0,0 +1,31 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "crypto2"
version = "0.2.0"
source = "git+https://github.com/shadowsocks/crypto2#dbfd3bb4751abd9945370dee25350765039d050a"
dependencies = [
"cfg-if",
]
[[package]]
name = "crypto2-demo"
version = "0.1.0"
dependencies = [
"crypto2",
"hex",
]
[[package]]
name = "hex"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"

View File

@@ -0,0 +1,10 @@
[package]
name = "crypto2-demo"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
crypto2 = { version = "0.2.0", git = "https://github.com/shadowsocks/crypto2" }
hex = "0.4.3"

View File

@@ -0,0 +1,24 @@
use crypto2::blockcipher::Aes128;
fn main() {
let key = [
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
0x0f,
];
let plaintext = [
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee,
0xff,
];
let cipher = Aes128::new(&key);
let mut ciphertext = plaintext.clone();
cipher.encrypt(&mut ciphertext);
let mut cleartext = ciphertext.clone();
cipher.decrypt(&mut cleartext);
println!("plaintext : {:?}", &plaintext[..]);
println!("ciphertext: {:?}", &ciphertext[..]);
println!("cleartext : {:?}", &cleartext[..]);
}

View File

@@ -1,5 +1,13 @@
# This file is automatically @generated by Cargo. # This file is automatically @generated by Cargo.
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3
[[package]]
name = "bech32"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d86b93f97252c47b41663388e6d155714a9d0c398b99f1005cbc5f978b29f445"
[[package]] [[package]]
name = "block-buffer" name = "block-buffer"
version = "0.9.0" version = "0.9.0"
@@ -37,6 +45,7 @@ checksum = "8aebca1129a03dc6dc2b127edd729435bbc4a37e1d5f4d7513165089ceb02634"
name = "curve25519" name = "curve25519"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"bech32",
"curve25519-dalek", "curve25519-dalek",
"ed25519-dalek", "ed25519-dalek",
"hex", "hex",

View File

@@ -12,3 +12,4 @@ rand = "0.7.3"
x25519-dalek = "1.1.0" x25519-dalek = "1.1.0"
ed25519-dalek = "1.0.1" ed25519-dalek = "1.0.1"
curve25519-dalek = "3.0.0" curve25519-dalek = "3.0.0"
bech32 = "0.9.1"

View File

@@ -1,9 +1,12 @@
use std::convert::TryInto;
use bech32::{ToBase32, Variant};
use ed25519_dalek::{Keypair, PublicKey as EdPublicKey, Signature, Signer, Verifier};
use rand::rngs::OsRng; use rand::rngs::OsRng;
use ed25519_dalek::{ Keypair, Signer, Verifier }; use x25519_dalek::{EphemeralSecret, PublicKey};
use x25519_dalek::{ EphemeralSecret, PublicKey };
fn main() { fn main() {
let mut csprng = OsRng{}; let mut csprng = OsRng {};
let keypair = Keypair::generate(&mut csprng); let keypair = Keypair::generate(&mut csprng);
println!("Key pair: {:#?}", keypair); println!("Key pair: {:#?}", keypair);
@@ -15,6 +18,7 @@ fn main() {
println!("Verify result: {:?}", public_key.verify(message, &signature)); println!("Verify result: {:?}", public_key.verify(message, &signature));
// key exchange // key exchange
println!("{}", "-".repeat(88));
let alice_secret = EphemeralSecret::new(OsRng); let alice_secret = EphemeralSecret::new(OsRng);
let alice_public = PublicKey::from(&alice_secret); let alice_public = PublicKey::from(&alice_secret);
let bob_secret = EphemeralSecret::new(OsRng); let bob_secret = EphemeralSecret::new(OsRng);
@@ -23,6 +27,40 @@ fn main() {
let alice_shared_secret = alice_secret.diffie_hellman(&bob_public); let alice_shared_secret = alice_secret.diffie_hellman(&bob_public);
let bob_shared_secret = bob_secret.diffie_hellman(&alice_public); let bob_shared_secret = bob_secret.diffie_hellman(&alice_public);
println!("Alice public key : {}", hex::encode(alice_public.as_bytes()));
println!("Bob public key : {}", hex::encode(bob_public.as_bytes()));
println!("Alice shared secret: {}", hex::encode(alice_shared_secret.as_bytes())); println!("Alice shared secret: {}", hex::encode(alice_shared_secret.as_bytes()));
println!("Bob shared secret: {}", hex::encode(bob_shared_secret.as_bytes())); println!("Bob shared secret: {}", hex::encode(bob_shared_secret.as_bytes()));
println!("{}", "-".repeat(88));
let public_key_bytes = hex::decode("7FEBAAB0D80CED24730B613F3D86924560EBCF13A838DEBC065F63C69C24C61E").unwrap();
let public_key_bytes: [u8; 32] = public_key_bytes.try_into().unwrap();
let public_key_card = PublicKey::from(public_key_bytes);
println!("Public key card: {}", hex::encode(public_key_card.as_bytes()));
let age_addr = bech32::encode(
"age",
public_key_card.as_bytes().to_base32(),
Variant::Bech32,
).unwrap();
println!("Public key card as age address: {}", age_addr);
let new_secret = EphemeralSecret::new(OsRng);
let new_public = PublicKey::from(&new_secret);
println!("New public key: {}", hex::encode(new_public.as_bytes()));
let new_card_shared_secret = new_secret.diffie_hellman(&public_key_card);
println!("New&card shared secret: {}", hex::encode(new_card_shared_secret.as_bytes()));
println!("{}", "-".repeat(88));
// $ cargo r -- pgp-card-sign -2 1dc91001cb69b0dbfab0347d684d7551227484f57a5ac414680414b855051159 --algo eddsa
// [OK ] User sign pin verify success!
// [OK ] SHA256 signature HEX: 4b2355918b92ca8e5490a30775aa4603a76efe344e3eecfd888b10018a5cb86d3d7095c298b34b39c4e1d46841098dd2514101a9eba5b7a271cf9b5c9d078306
let public_key_bytes = hex::decode("d57a081448c6d3d6076b1cca0216f319e45cc2ad986541caf0d3d62f5b318263").unwrap();
let public_key_card = EdPublicKey::from_bytes(&public_key_bytes).unwrap();
let message = hex::decode("1dc91001cb69b0dbfab0347d684d7551227484f57a5ac414680414b855051159").unwrap();
let signature = hex::decode("4b2355918b92ca8e5490a30775aa4603a76efe344e3eecfd888b10018a5cb86d3d7095c298b34b39c4e1d46841098dd2514101a9eba5b7a271cf9b5c9d078306").unwrap();
let signature_bytes: [u8; 64] = signature.try_into().unwrap();
let signature = Signature::new(signature_bytes);
let verify_result = public_key_card.verify(&message, &signature);
println!("Verify result: {:?}", verify_result);
} }

158
__crypto/efficient_sm2/Cargo.lock generated Normal file
View File

@@ -0,0 +1,158 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "autocfg"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "byteorder"
version = "1.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "efficient-sm2"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1eb543143510430fc814e04178d42bbbfaee821d6546196a87a527bd7ccd6bd6"
dependencies = [
"libsm",
"rand",
]
[[package]]
name = "efficient_sm2"
version = "0.1.0"
dependencies = [
"efficient-sm2",
"rand",
]
[[package]]
name = "getrandom"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.137"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc7fcc620a3bff7cdd7a365be3376c97191aeaccc2a603e600951e452615bf89"
[[package]]
name = "libsm"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "47aefac398c4d7ed78ea6b74bc692ebe27d3da99ca6deffa76cabf05d5864c4a"
dependencies = [
"byteorder",
"lazy_static",
"num-bigint",
"num-integer",
"num-traits",
"rand",
"yasna",
]
[[package]]
name = "num-bigint"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f93ab6289c7b344a8a9f60f88d80aa20032336fe78da341afc91c8a2341fc75f"
dependencies = [
"autocfg",
"num-integer",
"num-traits",
]
[[package]]
name = "num-integer"
version = "0.1.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9"
dependencies = [
"autocfg",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd"
dependencies = [
"autocfg",
]
[[package]]
name = "ppv-lite86"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom",
]
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "yasna"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e262a29d0e61ccf2b6190d7050d4b237535fc76ce4c1210d9caa316f71dffa75"
dependencies = [
"num-bigint",
]

View File

@@ -0,0 +1,11 @@
[package]
name = "efficient_sm2"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rand = "0.8.5"
efficient-sm2 = "0.1.5"

View File

@@ -0,0 +1,17 @@
use rand::RngCore;
fn main() {
let test_word = b"hello world";
let mut private_key = [0; 32];
rand::thread_rng().fill_bytes(&mut private_key);
let key_pair = efficient_sm2::KeyPair::new(&private_key).unwrap();
// signing in sm2
let sig = key_pair.sign(test_word).unwrap();
// verification sm2 signature
sig.verify(&key_pair.public_key(), test_word).unwrap();
}

317
__crypto/eth-address/Cargo.lock generated Normal file
View File

@@ -0,0 +1,317 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "arrayvec"
version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50"
[[package]]
name = "bitcoin-io"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b47c4ab7a93edb0c7198c5535ed9b52b63095f4e9b45279c6736cec4b856baf"
[[package]]
name = "bitcoin_hashes"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb18c03d0db0247e147a21a6faafd5a7eb851c743db062de72018b6b7e8e4d16"
dependencies = [
"bitcoin-io",
"hex-conservative",
]
[[package]]
name = "bitflags"
version = "2.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967"
[[package]]
name = "byteorder"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
[[package]]
name = "cc"
version = "1.2.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d487aa071b5f64da6f19a3e848e3578944b726ee5a4854b82172f02aa876bfdc"
dependencies = [
"shlex",
]
[[package]]
name = "cfg-if"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9555578bc9e57714c812a1f84e4fc5b4d21fcb063490c624de019f7464c91268"
[[package]]
name = "crunchy"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43da5946c66ffcc7745f48db692ffbb10a83bfe0afd96235c5c2a4fb23994929"
[[package]]
name = "eth-address"
version = "0.1.0"
dependencies = [
"hex",
"keccak-hash",
"num_cpus",
"rand",
"secp256k1",
]
[[package]]
name = "fixed-hash"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "835c052cb0c08c1acf6ffd71c022172e18723949c8282f2b9f27efbc51e64534"
dependencies = [
"static_assertions",
]
[[package]]
name = "getrandom"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26145e563e54f2cadc477553f1ec5ee650b00862f0a58bcd12cbdc5f0ea2d2f4"
dependencies = [
"cfg-if",
"libc",
"r-efi",
"wasi",
]
[[package]]
name = "hermit-abi"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc0fef456e4baa96da950455cd02c081ca953b141298e41db3fc7e36b1da849c"
[[package]]
name = "hex"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
[[package]]
name = "hex-conservative"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5313b072ce3c597065a808dbf612c4c8e8590bdbf8b579508bf7a762c5eae6cd"
dependencies = [
"arrayvec",
]
[[package]]
name = "keccak-hash"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3e1b8590eb6148af2ea2d75f38e7d29f5ca970d5a4df456b3ef19b8b415d0264"
dependencies = [
"primitive-types",
"tiny-keccak",
]
[[package]]
name = "libc"
version = "0.2.174"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1171693293099992e19cddea4e8b849964e9846f4acee11b3948bcc337be8776"
[[package]]
name = "num_cpus"
version = "1.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "91df4bbde75afed763b708b7eee1e8e7651e02d97f6d5dd763e89367e957b23b"
dependencies = [
"hermit-abi",
"libc",
]
[[package]]
name = "ppv-lite86"
version = "0.2.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9"
dependencies = [
"zerocopy",
]
[[package]]
name = "primitive-types"
version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d15600a7d856470b7d278b3fe0e311fe28c2526348549f8ef2ff7db3299c87f5"
dependencies = [
"fixed-hash",
"uint",
]
[[package]]
name = "proc-macro2"
version = "1.0.95"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d"
dependencies = [
"proc-macro2",
]
[[package]]
name = "r-efi"
version = "5.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f"
[[package]]
name = "rand"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9fbfd9d094a40bf3ae768db9361049ace4c0e04a4fd6b359518bd7b73a73dd97"
dependencies = [
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99d9a13982dcf210057a8a78572b2217b667c3beacbf3a0d8b454f6f82837d38"
dependencies = [
"getrandom",
]
[[package]]
name = "secp256k1"
version = "0.31.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c3c81b43dc2d8877c216a3fccf76677ee1ebccd429566d3e67447290d0c42b2"
dependencies = [
"bitcoin_hashes",
"rand",
"secp256k1-sys",
]
[[package]]
name = "secp256k1-sys"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcb913707158fadaf0d8702c2db0e857de66eb003ccfdda5924b5f5ac98efb38"
dependencies = [
"cc",
]
[[package]]
name = "shlex"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]]
name = "static_assertions"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
[[package]]
name = "syn"
version = "2.0.104"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17b6f705963418cdb9927482fa304bc562ece2fdd4f616084c50b7023b435a40"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "tiny-keccak"
version = "2.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c9d3793400a45f954c52e73d068316d76b6f4e36977e3fcebb13a2721e80237"
dependencies = [
"crunchy",
]
[[package]]
name = "uint"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "909988d098b2f738727b161a106cfc7cab00c539c2687a8836f8e565976fb53e"
dependencies = [
"byteorder",
"crunchy",
"hex",
"static_assertions",
]
[[package]]
name = "unicode-ident"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"
[[package]]
name = "wasi"
version = "0.14.2+wasi-0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9683f9a5a998d873c0d21fcbe3c083009670149a8fab228644b8bd36b2c48cb3"
dependencies = [
"wit-bindgen-rt",
]
[[package]]
name = "wit-bindgen-rt"
version = "0.39.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1"
dependencies = [
"bitflags",
]
[[package]]
name = "zerocopy"
version = "0.8.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1039dd0d3c310cf05de012d8a39ff557cb0d23087fd44cad61df08fc31907a2f"
dependencies = [
"zerocopy-derive",
]
[[package]]
name = "zerocopy-derive"
version = "0.8.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ecf5b4cc5364572d7f4c329661bcc82724222973f2cab6f050a4e5c22f75181"
dependencies = [
"proc-macro2",
"quote",
"syn",
]

View File

@@ -0,0 +1,11 @@
[package]
name = "eth-address"
version = "0.1.0"
edition = "2024"
[dependencies]
hex = "0.4.3"
keccak-hash = "0.11.0"
num_cpus = "1.17.0"
rand = "0.9.1"
secp256k1 = { version = "0.31.1", features = ["rand"] }

View File

@@ -0,0 +1,157 @@
use keccak_hash::keccak_256;
use secp256k1::{PublicKey, Secp256k1};
use std::{
sync::{Arc, Mutex},
thread,
};
fn main() {
let args = std::env::args();
if args.len() <= 1 {
println!("[ERROR] Need at least one argument, e.g. 0xff 0xffff ...");
return;
}
let continue_on_found = std::env::var("CONTINUE_ON_FOUND")
.map(|c| c.to_lowercase())
.map(|c| c == "1" || c == "true" || c == "yes" || c == "on")
.unwrap_or_else(|_| false);
let prefixes: Vec<String> = args.skip(1).collect();
println!("Prefixes: {:?}", prefixes);
if continue_on_found {
println!("CONTINUE_ON_FOUND is ON");
} else {
println!("CONTINUE_ON_FOUND is OFF");
}
let loop_count = std::env::var("LOOP_COUNT")
.map(|c| c.parse::<u64>())
.unwrap_or_else(|_| Ok(10_000_000_u64))
.unwrap_or_else(|_| 10_000_000_u64);
println!("LOOP_COUNT={}", loop_count);
let num_of_vcpus = num_cpus::get();
let num_of_phycpus = num_cpus::get_physical();
println!(
"You have {} vCPUs, from {} phyCPUs",
num_of_vcpus, num_of_phycpus
);
let concurrent_count = std::env::var("CONCURRENT_COUNT")
.map(|c| c.parse::<u64>())
.unwrap_or_else(|_| Ok(num_of_phycpus as u64))
.unwrap_or_else(|_| 1_u64);
println!("CONCURRENT_COUNT={}", concurrent_count);
let stop_flag = Arc::new(Mutex::new(false));
let mut handles = vec![];
for ind in 0..concurrent_count {
println!("- Running task {} of {}", ind, concurrent_count);
let the_stop_flag = Arc::clone(&stop_flag);
let prefixes = prefixes.clone();
let child = thread::spawn(move || {
run_one_task(prefixes, continue_on_found, loop_count, ind, the_stop_flag);
});
handles.push(child);
}
while let Some(h) = handles.pop() {
h.join().unwrap();
}
println!("Finished!");
}
fn run_one_task(
prefixes: Vec<String>,
continue_on_found: bool,
loop_count: u64,
ind: u64,
the_stop_flag: Arc<Mutex<bool>>,
) {
let secp = Secp256k1::new();
for i in 0..loop_count {
let (secret_key, public_key) = secp.generate_keypair(&mut rand::rng());
let s = make_eth_address(&public_key);
if i % 1_000 == 0 {
if *the_stop_flag.lock().unwrap() {
return;
}
if i > 0 && i % 100_000 == 0 {
println!("> {} - {}", ind, i);
}
}
let lower_s = s.to_ascii_lowercase();
if prefixes.iter().any(|p| lower_s.starts_with(p)) {
println!(">> {}\n{}", s, hex::encode(secret_key.as_ref()));
if !continue_on_found {
*the_stop_flag.lock().unwrap() = true;
break;
}
}
}
}
// reference: https://eips.ethereum.org/EIPS/eip-55
fn make_eth_address(public_key: &PublicKey) -> String {
let public_key_bytes = public_key.serialize_uncompressed().to_vec();
let mut output = [0_u8; 32];
keccak_256(&public_key_bytes[1..], &mut output);
let addr_without_prefix_0x = hex::encode(&output[32 - 20..]);
let mut addr_keccak_256_output = [0_u8; 32];
keccak_256(
addr_without_prefix_0x.as_bytes(),
&mut addr_keccak_256_output,
);
let addr_keccak_256_hex = hex::encode(&addr_keccak_256_output);
let mut addr = String::with_capacity(42);
addr.push_str("0x");
let addr_keccak_256_chars = addr_keccak_256_hex.chars().collect::<Vec<_>>();
let addr_chars = addr_without_prefix_0x.chars().collect::<Vec<_>>();
for i in 0..addr_chars.len() {
if addr_keccak_256_chars[i] >= '8' {
addr.push(addr_chars[i].to_ascii_uppercase());
} else {
addr.push(addr_chars[i])
}
}
addr
}
#[test]
fn test_make_eth_address() {
use secp256k1::{SecretKey};
for (private_key, eth_address) in [
(
"961fc1985ceca1e23b169103e041ecfa642ff8690a6ecc59d91f2dba648ab02b",
"0x0005119dEE1fD8763fE6ADBbc801484a3728F0db",
),
(
"2fa0db47c5939d7d4831d05dcdb329f6e7f7d5ca2c7247a666729aa513ea26bf",
"0x23aEf078e0997434Eb47E6d0B3C7770010473456",
),
(
"e6a8f02f9d308eca44b8437d6dc1d08101449ecef5851dfe5093bcf0bc21fa5b",
"0xef87E9712c412ef7B90804EbaB763FAe24E10123",
),
(
"3d541861865959e1e1c303d5c2a90084192415b61ead3fdb778047278011170b",
"0x1451449D05A3C232a543A995C2A38c07095f6666",
),
] {
let temp_key = hex::decode(private_key).unwrap();
let mut key_32_bytes = [0_u8; 32];
for i in 0..32 {
key_32_bytes[i] = temp_key[i];
}
let secp = Secp256k1::new();
let secret_key =
SecretKey::from_byte_array(key_32_bytes).expect("32 bytes, within curve order");
let public_key = PublicKey::from_secret_key(&secp, &secret_key);
assert_eq!(eth_address, make_eth_address(&public_key));
}
}

16
__crypto/highwayhash/Cargo.lock generated Normal file
View File

@@ -0,0 +1,16 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "highway"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3688f13e7735ae5ca93edc490f6c5885b77d473255bc1fed92b9c805f972bf94"
[[package]]
name = "highwayhash"
version = "0.1.0"
dependencies = [
"highway",
]

View File

@@ -0,0 +1,10 @@
[package]
name = "highwayhash"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
highway = "0.8.0"

View File

@@ -0,0 +1,5 @@
* https://github.com/nickbabcock/highway-rs
* https://github.com/rurban/smhasher

View File

@@ -0,0 +1,12 @@
use highway::{HighwayHash, HighwayHasher, Key};
fn main() {
let res = HighwayHasher::default().hash256("hello world".as_bytes());
println!("{:x?}", res);
let res = HighwayHasher::default().hash256(&[0xff]);
println!("{:x?}", res);
let mut hasher256 = HighwayHasher::new(Key([1, 2, 3, 4]));
hasher256.append(&[0xff]);
let res: [u64; 4] = hasher256.finalize256();
println!("{:x?}", res);
}

117
__crypto/hmac_sha1/Cargo.lock generated Normal file
View File

@@ -0,0 +1,117 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "block-buffer"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0bf7fe51849ea569fd452f37822f606a5cabb684dc918707a0193fd4664ff324"
dependencies = [
"generic-array",
]
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "cpufeatures"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59a6001667ab124aebae2a495118e11d30984c3a653e99d86d58971708cf5e4b"
dependencies = [
"libc",
]
[[package]]
name = "crypto-common"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57952ca27b5e3606ff4dd79b0020231aaf9d6aa76dc05fd30137538c50bd3ce8"
dependencies = [
"generic-array",
"typenum",
]
[[package]]
name = "digest"
version = "0.10.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2fb860ca6fafa5552fb6d0e816a69c8e49f0908bf524e30a90d97c85892d506"
dependencies = [
"block-buffer",
"crypto-common",
"subtle",
]
[[package]]
name = "generic-array"
version = "0.14.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd48d33ec7f05fbfa152300fdad764757cbded343c1aa1cff2fbaf4134851803"
dependencies = [
"typenum",
"version_check",
]
[[package]]
name = "hex"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
[[package]]
name = "hmac"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e"
dependencies = [
"digest",
]
[[package]]
name = "hmac_sha1"
version = "0.1.0"
dependencies = [
"hex",
"hmac",
"sha-1",
]
[[package]]
name = "libc"
version = "0.2.121"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "efaa7b300f3b5fe8eb6bf21ce3895e1751d9665086af2d64b42f19701015ff4f"
[[package]]
name = "sha-1"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "028f48d513f9678cda28f6e4064755b3fbb2af6acd672f2c209b62323f7aea0f"
dependencies = [
"cfg-if",
"cpufeatures",
"digest",
]
[[package]]
name = "subtle"
version = "2.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6bdef32e8150c2a081110b42772ffe7d7c9032b606bc226c8260fd97e0976601"
[[package]]
name = "typenum"
version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcf81ac59edc17cc8697ff311e8f5ef2d99fcbd9817b34cec66f90b6c3dfd987"
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"

View File

@@ -0,0 +1,11 @@
[package]
name = "hmac_sha1"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
sha-1 = "*"
hmac = "*"
hex = "*"

View File

@@ -0,0 +1,21 @@
use hmac::{Hmac, Mac};
use sha1::Sha1;
const SHA1_DIGEST_SIZE: usize = 20;
type HmacSha1 = Hmac<Sha1>;
fn main() {
let args = std::env::args().into_iter().collect::<Vec<String>>();
println!("Args: {:?}", args);
let key = hex::decode(&args[1]).expect("Key hex parse failed");
let data = hex::decode(&args[2]).expect("Data hex parse failed");
let mut hmac_sha1 = HmacSha1::new_from_slice(&key).unwrap();
hmac_sha1.update(&data);
let result = hmac_sha1.finalize();
let mut code = [0; SHA1_DIGEST_SIZE];
code.copy_from_slice(result.into_bytes().as_slice());
println!("{}", hex::encode(&code));
}

1357
__crypto/jose-test/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,20 @@
[package]
name = "jose-test"
version = "0.1.0"
edition = "2021"
[dependencies]
aes-gcm-stream = "0.2.3"
aes-kw = { version = "0.2.1", features = ["alloc"] }
base64 = "0.22.1"
biscuit = "0.7.0"
#jose-jwe = "0.0.0"
jose-jwk = { version = "0.1.2", features = ["rsa"] }
josekit = "0.10.0"
rand = { version = "0.8.5", features = [] }
rsa = "0.9.6"
rust_util = "0.6.47"
serde = "1.0.214"
serde_json = "1.0.132"
sha1 = "0.10.6"
sha2 = "0.10.8"

View File

@@ -0,0 +1,241 @@
use aes_gcm_stream::{Aes256GcmStreamDecryptor, Aes256GcmStreamEncryptor};
use aes_kw::Kek;
use base64::engine::general_purpose::URL_SAFE_NO_PAD;
use base64::Engine;
use josekit::jwe;
use josekit::jwe::alg::aeskw::AeskwJweAlgorithm;
use josekit::jwe::alg::rsaes::RsaesJweAlgorithm;
use josekit::jwe::JweHeader;
use josekit::jwk::alg::rsa::RsaKeyPair;
use josekit::jwk::Jwk;
use rand::{random, thread_rng};
use rsa::{Oaep, RsaPrivateKey, RsaPublicKey};
use rust_util::{opt_result, simple_error, XResult};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use sha1::Sha1;
const LOCAL_KMS_PREFIX: &str = "LKMS:";
// JWE format:
// BASE64URL(UTF8(JWE Protected Header)) || '.' ||
// BASE64URL(JWE Encrypted Key) || '.' || BASE64URL(JWE Initialization Vector)
// || '.' || BASE64URL(JWE Ciphertext) || '.' || BASE64URL(JWE Authentication Tag).
//
// RSA JWE Header:
// {"enc":"A256GCM","vendor":"local-mini-kms","alg":"RSA-OAEP"}
// eyJlbmMiOiJBMjU2R0NNIiwidmVuZG9yIjoibG9jYWwtbWluaS1rbXMiLCJhbGciOiJSU0EtT0FFUCJ9.VQ_R
// yGjXqQlUIbRIMgaYRSaX5FMRBzZ6ApfdZ2yAwiG70hjNfR3ss7x4PYqMm6QtITm1O4_fp7I3bY8iUz5Njyth_
// Min7Xm2-WsQ6gq9yN58btkUBFm60c7SC5XLaqE1pEtBAz7786bJk6M4NeOtDAOFAmIb2j1EwnS5vweBtmNv7N
// UFIgvx806T3WkCFDOkMSJ10_6LSa0z-lIac-s68svsU5WW8CXVKxHAbxaHyX_otu2HxXzDZlF5Goamh5ZJtr0
// 0yW_bzDCx3hZ2nMK3Ve7IJ2ZLAMmvhj9LKWkPtoqH0dGHaPHWff5P3rZ4vtKywt_h5b6SYII_mEoJcpByMyGw
// TXCtZymDt82Tyv_FesW2721JgyGxnukuOxQRTw4MfGYIO5bldL3uGGI_H4HXlXhM_kp3wuPAZ0vH4Jj2KD6MV
// DDTJQaEBQIEF07i7WiNynr57kbahYwextRXYP7LgoUHfFwA5GGGpN-UkuWLlKkYLTmXGrPYnL6Cf9D3euP7nF
// ml2oA2hjig-UuYf9A_QSEqNsMxYDuG-rggn3H_iXNl4ooYcxSVOXhTKfoV578MkNwG75BdHN5FeRYIKq0HCTM
// lGqqBWmDibPtMd7Uq1JrDd8774lnA8JcZcCMSia4m6WJSbG0kOuJ4NJPOUrYtNEJXgWKU3FQzDB-apLMQdac.
// WYJgsdZRLk310KWd.P333-S2VYg.PCfruTdk8vh3a8wcjJCe-g
//
// RSA-OAEP RSA using Optimal Asymmetric Encryption Padding (OAEP), as defined in RFC 3447 [RFC3447]
// A256GCM Advanced Encryption Standard (AES) using 256 bit keys in Galois/Counter Mode, as defined in [FIPS197] and [NIST80038D]
//
// AES JWE Header:
// {"enc":"A256GCM","vendor":"local-mini-kms","version":"5b90f66a1c6a918d","alg":"A256KW"}
// eyJlbmMiOiJBMjU2R0NNIiwidmVuZG9yIjoibG9jYWwtbWluaS1rbXMiLCJ2ZXJzaW9uIjoiNWI5MGY2NmExYz
// ZhOTE4ZCIsImFsZyI6IkEyNTZLVyJ9.K2_P-b_Gq9wbrssbcS5AmiUwcnNTnnZSe7rBI1SixVrC7TfFK0fruw.
// ez3OKjOHAIIYnfM0.wSO3aXo.-vGJwk8JQKhi3voIlAA9gQ
//
// A256GCM Advanced Encryption Standard (AES) using 256 bit keys in Galois/Counter Mode, as defined in [FIPS197] and [NIST80038D]
// A256KW Advanced Encryption Standard (AES) Key Wrap Algorithm using 256 bit keys, as defined in RFC 3394 [RFC3394]
// JWE Header: {"alg":"dir","enc":"A256GCM"}
// Encrypted key (CEK): (blank)
// IV: Vlf_WdLm-spHbfJe
// Ciphertext: RxMPrw
// Authentication Tag: 5VC8Y_qSPdSubbGNGyfn6A
//
// JWE Header: {"alg":"A256KW","enc":"A256GCM"}
// Encrypted key (CEK): 66xZoxFI18zfvLMO6WU1zzqqX1tT8xu_qZzMQyPcfVuajPNkOJUXQA
// IV: X5ZL8yaOektXmfny
// Ciphertext: brz-Lg
// Authentication Tag: xG-EvM-9hrw0XRiuRW7HrA
//
// https://security.stackexchange.com/questions/80966/what-is-the-point-of-aes-key-wrap-with-json-web-encryption
const JWE_ENC_A256GCM: &str = "A256GCM";
const JWE_ALG_A256KW: &str = "A256KW";
const JWE_ALG_RSA_OAEP: &str = "RSA-OAEP";
const JWE_DOT: &str = ".";
pub fn generate_rsa_key_2(bits: u32) -> XResult<RsaPrivateKey> {
let mut rng = thread_rng();
Ok(RsaPrivateKey::new(&mut rng, bits as usize)?)
}
pub fn generate_rsa_key(bits: u32) -> XResult<RsaKeyPair> {
Ok(RsaKeyPair::generate(bits)?)
}
pub fn serialize_jwe_rsa_2(payload: &[u8], rsa_public_key: &RsaPublicKey) -> XResult<String> {
let header = JweHeader2 {
enc: JWE_ENC_A256GCM.to_string(),
alg: JWE_ALG_RSA_OAEP.to_string(),
vendor: "local-mini-kms".to_string(),
version: None,
};
serialize_jwe_fn(&header, payload, |data_key| -> XResult<Vec<u8>> {
let mut r = thread_rng();
Ok(opt_result!(rsa_public_key.encrypt(&mut r, Oaep::new::<Sha1>(), data_key), "Wrap key failed: {}"))
})
}
pub fn serialize_jwe_rsa(payload: &[u8], jwk: &Jwk) -> XResult<String> {
let mut header = JweHeader::new();
header.set_content_encryption(JWE_ENC_A256GCM);
header.set_claim("vendor", Some(Value::String("local-mini-kms".to_string())))?;
let encrypter = RsaesJweAlgorithm::RsaOaep.encrypter_from_jwk(jwk)?;
Ok(format!("{}{}", LOCAL_KMS_PREFIX, jwe::serialize_compact(payload, &header, &encrypter)?))
}
pub fn deserialize_jwe_rsa_2(jwe: &str, rsa: &RsaPrivateKey) -> XResult<(Vec<u8>, JweHeader2)> {
deserialize_jwe_fn(jwe, |alg, key_wrap| -> XResult<Vec<u8>> {
if alg != JWE_ALG_RSA_OAEP {
return simple_error!("Invalid JWE header alg: {}", alg);
}
Ok(opt_result!(rsa.decrypt(Oaep::new::<Sha1>(), &key_wrap), "Unwrap key failed: {}"))
})
}
pub fn deserialize_jwe_rsa(jwe: &str, jwk: &Jwk) -> XResult<(Vec<u8>, JweHeader)> {
let decrypter = RsaesJweAlgorithm::RsaOaep.decrypter_from_jwk(jwk)?;
Ok(jwe::deserialize_compact(&get_jwe(jwe), &decrypter)?)
}
#[derive(Debug, Serialize, Deserialize)]
pub struct JweHeader2 {
pub enc: String,
pub alg: String,
pub vendor: String,
pub version: Option<String>,
}
pub fn serialize_jwe_aes_2(payload: &[u8], key: [u8; 32]) -> XResult<String> {
let header = JweHeader2 {
enc: JWE_ENC_A256GCM.to_string(),
alg: JWE_ALG_A256KW.to_string(),
vendor: "local-mini-kms".to_string(),
version: None,
};
serialize_jwe_fn(&header, payload, |data_key| -> XResult<Vec<u8>> {
let kek = Kek::from(key);
Ok(opt_result!(kek.wrap_vec(&data_key[..]), "Wrap key failed: {}"))
})
}
pub fn serialize_jwe_aes(payload: &[u8], key: &[u8]) -> XResult<String> {
let mut header = JweHeader::new();
header.set_content_encryption(JWE_ENC_A256GCM);
header.set_claim("vendor", Some(Value::String("local-mini-kms".to_string())))?;
// header.set_claim("version", Some(Value::String(get_master_key_checksum(key))))?;
let encrypter = AeskwJweAlgorithm::A256kw.encrypter_from_bytes(key)?;
Ok(format!("{}{}", LOCAL_KMS_PREFIX, jwe::serialize_compact(payload, &header, &encrypter)?))
}
pub fn deserialize_jwe_aes_2(jwe: &str, key: [u8; 32]) -> XResult<(Vec<u8>, JweHeader2)> {
deserialize_jwe_fn(jwe, |alg, key_wrap| -> XResult<Vec<u8>> {
if alg != JWE_ALG_A256KW {
return simple_error!("Invalid JWE header alg: {}", alg);
}
let kek = Kek::from(key);
Ok(opt_result!(kek.unwrap_vec(&key_wrap), "Unwrap key failed: {}"))
})
}
pub fn deserialize_jwe_aes(jwe: &str, key: &[u8]) -> XResult<(Vec<u8>, JweHeader)> {
let decrypter = AeskwJweAlgorithm::A256kw.decrypter_from_bytes(key)?;
Ok(jwe::deserialize_compact(&get_jwe(jwe), &decrypter)?)
}
fn serialize_jwe_fn<F>(header: &JweHeader2, payload: &[u8], key_wrap_fn: F) -> XResult<String>
where
F: Fn(&[u8]) -> XResult<Vec<u8>>,
{
let header_str = opt_result!(serde_json::to_string(&header), "Invalid JWE header: {}");
let header_b64 = URL_SAFE_NO_PAD.encode(header_str.as_bytes());
let data_key: [u8; 32] = random();
let iv: [u8; 12] = random();
let mut encryptor = Aes256GcmStreamEncryptor::new(data_key, &iv);
encryptor.init_adata(header_b64.as_bytes());
let mut ciphertext = encryptor.update(payload);
let (ciphertext_final, tag) = encryptor.finalize();
ciphertext.extend_from_slice(&ciphertext_final);
let cek = key_wrap_fn(&data_key)?;
Ok(format!(
"{}.{}.{}.{}.{}",
header_b64,
URL_SAFE_NO_PAD.encode(&cek),
URL_SAFE_NO_PAD.encode(&iv),
URL_SAFE_NO_PAD.encode(&ciphertext),
URL_SAFE_NO_PAD.encode(&tag)
))
}
fn deserialize_jwe_fn<F>(jwe: &str, key_unwrap_fn: F) -> XResult<(Vec<u8>, JweHeader2)>
where
F: Fn(&str, &[u8]) -> XResult<Vec<u8>>,
{
let jwe_parts = jwe.split(JWE_DOT).collect::<Vec<&str>>();
if jwe_parts.len() != 5 {
return simple_error!("Invalid JWE: {}", jwe);
}
let header_bytes = opt_result!(decode_url_safe_no_pad(jwe_parts[0]), "Invalid JWE header: {}, JWE: {}", jwe);
let header: JweHeader2 = opt_result!(serde_json::from_slice(&header_bytes), "Invalid JWE header: {}, JWE: {}", jwe);
if header.enc != JWE_ENC_A256GCM {
return simple_error!("Invalid JWE header enc: {}", header.enc);
}
let cek = opt_result!(decode_url_safe_no_pad(jwe_parts[1]), "Invalid JWE CEK: {}, JWE: {}", jwe);
let iv = opt_result!(decode_url_safe_no_pad(jwe_parts[2]), "Invalid JWE IV: {}, JWE: {}", jwe);
let ciphertext = opt_result!(decode_url_safe_no_pad(jwe_parts[3]), "Invalid JWE ciphertext: {}, JWE: {}", jwe);
let tag = opt_result!(decode_url_safe_no_pad(jwe_parts[4]), "Invalid JWE tag: {}, JWE: {}", jwe);
let data_key = key_unwrap_fn(&header.alg, &cek)?;
let data_key_b32 = opt_result!(to_bytes32(&data_key), "Invalid JWE CEK: {}, JWE: {}", jwe);
let mut decryptor = Aes256GcmStreamDecryptor::new(data_key_b32, &iv);
decryptor.init_adata(jwe_parts[0].as_bytes());
let mut plaintext = decryptor.update(&ciphertext);
let plaintext_2 = decryptor.update(&tag);
let plaintext_final = opt_result!(decryptor.finalize(), "Invalid JWE: {}, JWE: {}", jwe);
plaintext.extend_from_slice(&plaintext_2);
plaintext.extend_from_slice(&plaintext_final);
Ok((plaintext, header))
}
#[inline]
fn decode_url_safe_no_pad(s: &str) -> XResult<Vec<u8>> {
Ok(URL_SAFE_NO_PAD.decode(s.as_bytes())?)
}
#[inline]
fn to_bytes32(bytes: &[u8]) -> XResult<[u8; 32]> {
if bytes.len() != 32 {
return simple_error!("Not valid 32 bytes");
}
let mut ret = [0; 32];
for i in 0..32 {
ret[i] = bytes[i];
}
Ok(ret)
}
#[inline]
fn get_jwe(jwe: &str) -> String {
if jwe.starts_with(LOCAL_KMS_PREFIX) {
jwe.chars().skip(LOCAL_KMS_PREFIX.len()).collect()
} else {
jwe.to_string()
}
}

View File

@@ -0,0 +1,92 @@
mod jose;
use crate::jose::{deserialize_jwe_aes, deserialize_jwe_aes_2, deserialize_jwe_rsa_2, serialize_jwe_aes_2, serialize_jwe_rsa, serialize_jwe_rsa_2};
use base64::engine::general_purpose::STANDARD;
use base64::Engine;
use jose_jwk::{Jwk, Key, Rsa};
use rand::random;
use rand::rngs::ThreadRng;
use rsa::traits::{PrivateKeyParts, PublicKeyParts};
use rsa::{Oaep, Pkcs1v15Encrypt};
use sha2::Sha256;
fn main() {
let key: [u8; 32] = random();
let payload = b"hello world";
let e = serialize_jwe_aes_2(payload, key).unwrap();
println!("{}", e);
let (d, h) = deserialize_jwe_aes(&e, &key).unwrap();
println!("{:?}", d);
println!("{:?}", h);
let (d2, h2) = deserialize_jwe_aes_2(&e, key).unwrap();
println!("{:?}", d2);
println!("{:?}", h2);
let rsa_key = jose::generate_rsa_key_2(2048).unwrap();
let rsa_public_key = rsa_key.as_ref();
let public_rsa: Rsa = rsa_public_key.into();
// let rsa: Rsa = rsa_key.into();
// println!("{:?}", public_rsa);
// println!("{:?}", rsa);
let jwk = Jwk {
key: Key::Rsa(public_rsa),
prm: Default::default(),
};
let jwk_str = serde_json::to_string(&jwk).unwrap();
println!("{}", &jwk_str);
// let rsa: Rsa = serde_json::from_str(&jwk_str).unwrap();
let josekitjwk = josekit::jwk::Jwk::from_bytes(jwk_str.as_bytes()).unwrap();
let rsa_jwe = serialize_jwe_rsa(b"hello world 001", &josekitjwk).unwrap();
// let rsa_key_2: RsaPrivateKey = rsa.try_into().unwrap();
println!(">>> {}", rsa_jwe);
let rsa_jwe = rsa_jwe.chars().skip(5).collect::<String>();
let (dd, hh) = deserialize_jwe_rsa_2(&rsa_jwe, &rsa_key).unwrap();
println!("DD: {}", String::from_utf8_lossy(&dd));
println!("HH: {:?}", hh);
println!("-------------------------------------------------------------------------------------");
let rsa_jwe2 = serialize_jwe_rsa_2(b"hello world 003", rsa_public_key).unwrap();
println!(">>> {}", rsa_jwe2);
// let rsa_jwe2 = rsa_jwe2.chars().skip(5).collect::<String>();
let (ddd, hhh) = deserialize_jwe_rsa_2(&rsa_jwe2, &rsa_key).unwrap();
println!("DDD: {}", String::from_utf8_lossy(&ddd));
println!("HHH: {:?}", hhh);
println!("-------------------------------------------------------------------------------------");
main2();
}
fn main2() {
let rsa_jwk = r##"{
"kty":"RSA",
"e":"AQAB",
"n":"5xyyPQx8Z7AjVf8Mg4xTE6yN8efPHc0lCdO056R2lzaEQeFwVCbOxFCnmly2-HtE_F_Wd3-B0nxOmODIyC4T
kLS3hXuflZb-OH73GwYDRmfcvWorACm0hv_a3p8YCuXvZ9aQUPRXwLn1jmAnKJiVRtObZTmmG5TsO-Q-U1DJjKhuP
--4co9T434y3U9xGIBYAudQ8tn4QSuVInrO2svg4gb9PExLDBn_46yEiLn-q0TzvO-s2rZ0iCpfB0Gji3djQiF-op
WUjLeGIMLC-gwAOKB4PjlsJBPlIg3pYpRuUEDERIy7kxZ8PIe4Nrfli_sRwMknm9HgQaigu3euYcjxXG9CALTs_vR
a7lRdpEKQY1ysY_z0bjIk1v-erEyNflQimpDHXGxujVj1EUrYz2diu60acmLSEciThafweAupr_qyKXUutBsc_KPK
33n8KBIPjl8X7bX2z-dXMKXo312vfPb6iO1ZcoFXG2nkFzwvFUKYFHSGjuZTQKgiaVEhlbq7N-80BlynnkHLYGcbn
9MtrT8O2Fci1i2ByxyL6B3vB1D5J1VyZ7IBY5AD1GanK2uFmC9nAoFvjTQJt3k2TI_MiPHgb1fZkHu0r-yrf9W_Hr
PrhBTp5wbepRSC6hs-yTykLJKSE5CD43KbjoW5GCqOkkQMnVjvSY4FpAdTj8Qt7kk"
}"##;
let rsa_jwk = rsa_jwk.replace(" ", "");
let rsa_jwk = rsa_jwk.replace("\r", "");
let rsa_jwk = rsa_jwk.replace("\n", "");
println!("{}", rsa_jwk);
let rsa: Rsa = serde_json::from_str(&rsa_jwk).unwrap();
let rsa_public_key = rsa::RsaPublicKey::try_from(rsa).unwrap();
// println!("{:?}", rsa_public_key);
let mut r = ThreadRng::default();
let enc = rsa_public_key.encrypt(&mut r, Pkcs1v15Encrypt, b"aaaaaaaa").unwrap();
println!("{}", STANDARD.encode(&enc));
let enc2 = rsa_public_key.encrypt(&mut r, Oaep::new::<Sha256>(), b"").unwrap();
println!("{}", STANDARD.encode(&enc2));
}

433
__crypto/liboqs-rust-demo/Cargo.lock generated Normal file
View File

@@ -0,0 +1,433 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "aho-corasick"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
dependencies = [
"memchr",
]
[[package]]
name = "base64"
version = "0.22.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9475866fec1451be56a3c2400fd081ff546538961565ccb5b7142cbd22bc7a51"
[[package]]
name = "bindgen"
version = "0.68.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "726e4313eb6ec35d2730258ad4e15b547ee75d6afaa1361a922e78e59b7d8078"
dependencies = [
"bitflags",
"cexpr",
"clang-sys",
"lazy_static",
"lazycell",
"log",
"peeking_take_while",
"prettyplease",
"proc-macro2",
"quote",
"regex",
"rustc-hash",
"shlex",
"syn",
"which",
]
[[package]]
name = "bitflags"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1"
[[package]]
name = "build-deps"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "64f14468960818ce4f3e3553c32d524446687884f8e7af5d3e252331d8a87e43"
dependencies = [
"glob",
]
[[package]]
name = "cc"
version = "1.0.90"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8cd6604a82acf3039f1144f54b8eb34e91ffba622051189e71b781822d5ee1f5"
[[package]]
name = "cexpr"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766"
dependencies = [
"nom",
]
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "clang-sys"
version = "1.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "67523a3b4be3ce1989d607a828d036249522dd9c1c8de7f4dd2dae43a37369d1"
dependencies = [
"glob",
"libc",
"libloading",
]
[[package]]
name = "cmake"
version = "0.1.50"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a31c789563b815f77f4250caee12365734369f942439b7defd71e18a48197130"
dependencies = [
"cc",
]
[[package]]
name = "cstr_core"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd98742e4fdca832d40cab219dc2e3048de17d873248f83f17df47c1bea70956"
dependencies = [
"cty",
"memchr",
]
[[package]]
name = "cty"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b365fabc795046672053e29c954733ec3b05e4be654ab130fe8f1f94d7051f35"
[[package]]
name = "either"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "11157ac094ffbdde99aa67b23417ebdd801842852b500e395a45a9c0aac03e4a"
[[package]]
name = "errno"
version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245"
dependencies = [
"libc",
"windows-sys",
]
[[package]]
name = "glob"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b"
[[package]]
name = "home"
version = "0.5.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3d1354bf6b7235cb4a0576c2619fd4ed18183f689b12b006a0ee7329eeff9a5"
dependencies = [
"windows-sys",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "lazycell"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
[[package]]
name = "libc"
version = "0.2.153"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd"
[[package]]
name = "libloading"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c2a198fb6b0eada2a8df47933734e6d35d350665a33a3593d7164fa52c75c19"
dependencies = [
"cfg-if",
"windows-targets",
]
[[package]]
name = "liboqs-rust-demo"
version = "0.1.0"
dependencies = [
"base64",
"oqs",
]
[[package]]
name = "linux-raw-sys"
version = "0.4.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "01cda141df6706de531b6c46c3a33ecca755538219bd484262fa09410c13539c"
[[package]]
name = "log"
version = "0.4.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c"
[[package]]
name = "memchr"
version = "2.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149"
[[package]]
name = "minimal-lexical"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
[[package]]
name = "nom"
version = "7.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a"
dependencies = [
"memchr",
"minimal-lexical",
]
[[package]]
name = "once_cell"
version = "1.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
[[package]]
name = "oqs"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "311616b64e55f9dc110286ea7cc5e475216dc332c54291e59a709c32fddb3a14"
dependencies = [
"cstr_core",
"libc",
"oqs-sys",
]
[[package]]
name = "oqs-sys"
version = "0.9.1+liboqs-0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "afa79adc3c10f8e01d0b134c159254e5843ec3f91c0bd868e57777beb3329e17"
dependencies = [
"bindgen",
"build-deps",
"cmake",
"libc",
"pkg-config",
]
[[package]]
name = "peeking_take_while"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099"
[[package]]
name = "pkg-config"
version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec"
[[package]]
name = "prettyplease"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a41cf62165e97c7f814d2221421dbb9afcbcdb0a88068e5ea206e19951c2cbb5"
dependencies = [
"proc-macro2",
"syn",
]
[[package]]
name = "proc-macro2"
version = "1.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e835ff2298f5721608eb1a980ecaee1aef2c132bf95ecc026a11b7bf3c01c02e"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef"
dependencies = [
"proc-macro2",
]
[[package]]
name = "regex"
version = "1.10.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c117dbdfde9c8308975b6a18d71f3f385c89461f7b3fb054288ecf2a2058ba4c"
dependencies = [
"aho-corasick",
"memchr",
"regex-automata",
"regex-syntax",
]
[[package]]
name = "regex-automata"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "86b83b8b9847f9bf95ef68afb0b8e6cdb80f498442f5179a29fad448fcc1eaea"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f"
[[package]]
name = "rustc-hash"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
[[package]]
name = "rustix"
version = "0.38.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "65e04861e65f21776e67888bfbea442b3642beaa0138fdb1dd7a84a52dffdb89"
dependencies = [
"bitflags",
"errno",
"libc",
"linux-raw-sys",
"windows-sys",
]
[[package]]
name = "shlex"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]]
name = "syn"
version = "2.0.55"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "002a1b3dbf967edfafc32655d0f377ab0bb7b994aa1d32c8cc7e9b8bf3ebb8f0"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "which"
version = "4.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87ba24419a2078cd2b0f2ede2691b6c66d8e47836da3b6db8265ebad47afbfc7"
dependencies = [
"either",
"home",
"once_cell",
"rustix",
]
[[package]]
name = "windows-sys"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7dd37b7e5ab9018759f893a1952c9420d060016fc19a472b4bb20d1bdd694d1b"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bcf46cf4c365c6f2d1cc93ce535f2c8b244591df96ceee75d8e83deb70a9cac9"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da9f259dd3bcf6990b55bffd094c4f7235817ba4ceebde8e6d11cd0c5633b675"
[[package]]
name = "windows_i686_gnu"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b474d8268f99e0995f25b9f095bc7434632601028cf86590aea5c8a5cb7801d3"
[[package]]
name = "windows_i686_msvc"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1515e9a29e5bed743cb4415a9ecf5dfca648ce85ee42e15873c3cd8610ff8e02"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5eee091590e89cc02ad514ffe3ead9eb6b660aedca2183455434b93546371a03"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77ca79f2451b49fa9e2af39f0747fe999fcda4f5e241b2898624dca97a1f2177"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32b752e52a2da0ddfbdbcc6fceadfeede4c939ed16d13e648833a61dfb611ed8"

View File

@@ -0,0 +1,10 @@
[package]
name = "liboqs-rust-demo"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
base64 = "0.22.0"
oqs = "0.9.0"

View File

@@ -0,0 +1,35 @@
use base64::Engine;
use base64::engine::general_purpose::STANDARD;
use oqs::*;
// from: https://github.com/open-quantum-safe/liboqs-rust
fn main() -> Result<()> {
let sigalg = sig::Sig::new(sig::Algorithm::Dilithium2)?;
let kemalg = kem::Kem::new(kem::Algorithm::Kyber512)?;
// A's long-term secrets
let (a_sig_pk, a_sig_sk) = sigalg.keypair()?;
// B's long-term secrets
let (b_sig_pk, b_sig_sk) = sigalg.keypair()?;
// assumption: A has (a_sig_sk, a_sig_pk, b_sig_pk)
// assumption: B has (b_sig_sk, b_sig_pk, a_sig_pk)
// A -> B: kem_pk, signature
let (kem_pk, kem_sk) = kemalg.keypair()?;
let signature = sigalg.sign(kem_pk.as_ref(), &a_sig_sk)?;
println!(">>>>> Signature: {}\n", STANDARD.encode(&signature));
// B -> A: kem_ct, signature
sigalg.verify(kem_pk.as_ref(), &signature, &a_sig_pk)?;
let (kem_ct, b_kem_ss) = kemalg.encapsulate(&kem_pk)?;
let signature = sigalg.sign(kem_ct.as_ref(), &b_sig_sk)?;
println!(">>>>> Signature: {}\n", STANDARD.encode(&signature));
// A verifies, decapsulates, now both have kem_ss
sigalg.verify(kem_ct.as_ref(), &signature, &b_sig_pk)?;
let a_kem_ss = kemalg.decapsulate(&kem_sk, &kem_ct)?;
println!(">>>>> Shared secret: {}", STANDARD.encode(&a_kem_ss));
assert_eq!(a_kem_ss, b_kem_ss);
Ok(())
}

258
__crypto/ml-kem-demo/Cargo.lock generated Normal file
View File

@@ -0,0 +1,258 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "base64"
version = "0.22.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"
[[package]]
name = "block-buffer"
version = "0.10.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71"
dependencies = [
"generic-array",
]
[[package]]
name = "cfg-if"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2fd1289c04a9ea8cb22300a459a72a385d7c73d3259e2ed7dcb2af674838cfa9"
[[package]]
name = "cpufeatures"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280"
dependencies = [
"libc",
]
[[package]]
name = "crypto-common"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3"
dependencies = [
"generic-array",
"typenum",
]
[[package]]
name = "digest"
version = "0.10.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292"
dependencies = [
"block-buffer",
"crypto-common",
]
[[package]]
name = "generic-array"
version = "0.14.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a"
dependencies = [
"typenum",
"version_check",
]
[[package]]
name = "getrandom"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "335ff9f135e4384c8150d6f27c6daed433577f86b4750418338c01a1a2528592"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "hybrid-array"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2d35805454dc9f8662a98d6d61886ffe26bd465f5960e0e55345c70d5c0d2a9"
dependencies = [
"typenum",
]
[[package]]
name = "keccak"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ecc2af9a1119c51f12a14607e783cb977bde58bc069ff0c3da1095e635d70654"
dependencies = [
"cpufeatures",
]
[[package]]
name = "kem"
version = "0.3.0-pre.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b8645470337db67b01a7f966decf7d0bafedbae74147d33e641c67a91df239f"
dependencies = [
"rand_core",
"zeroize",
]
[[package]]
name = "libc"
version = "0.2.176"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "58f929b4d672ea937a23a1ab494143d968337a5f47e56d0815df1e0890ddf174"
[[package]]
name = "ml-kem"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "97befee0c869cb56f3118f49d0f9bb68c9e3f380dec23c1100aedc4ec3ba239a"
dependencies = [
"hybrid-array",
"kem",
"rand_core",
"sha3",
"zeroize",
]
[[package]]
name = "ml-kem-demo"
version = "0.1.0"
dependencies = [
"base64",
"ml-kem",
"rand",
]
[[package]]
name = "ppv-lite86"
version = "0.2.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9"
dependencies = [
"zerocopy",
]
[[package]]
name = "proc-macro2"
version = "1.0.101"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89ae43fd86e4158d6db51ad8e2b80f313af9cc74f5c0e03ccb87de09998732de"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d"
dependencies = [
"proc-macro2",
]
[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom",
]
[[package]]
name = "sha3"
version = "0.10.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75872d278a8f37ef87fa0ddbda7802605cb18344497949862c0d4dcb291eba60"
dependencies = [
"digest",
"keccak",
]
[[package]]
name = "syn"
version = "2.0.106"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ede7c438028d4436d71104916910f5bb611972c5cfd7f89b8300a8186e6fada6"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "typenum"
version = "1.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1dccffe3ce07af9386bfd29e80c0ab1a8205a2fc34e4bcd40364df902cfa8f3f"
[[package]]
name = "unicode-ident"
version = "1.0.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f63a545481291138910575129486daeaf8ac54aee4387fe7906919f7830c7d9d"
[[package]]
name = "version_check"
version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
[[package]]
name = "wasi"
version = "0.11.1+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b"
[[package]]
name = "zerocopy"
version = "0.8.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0894878a5fa3edfd6da3f88c4805f4c8558e2b996227a3d864f47fe11e38282c"
dependencies = [
"zerocopy-derive",
]
[[package]]
name = "zerocopy-derive"
version = "0.8.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "88d2b8d9c68ad2b9e4340d7832716a4d21a22a1154777ad56ea55c51a9cf3831"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "zeroize"
version = "1.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ced3678a2879b30306d323f4542626697a464a97c0a07c9aebf7ebca65cd4dde"

View File

@@ -0,0 +1,9 @@
[package]
name = "ml-kem-demo"
version = "0.1.0"
edition = "2024"
[dependencies]
base64 = "0.22.1"
ml-kem = { version = "0.2.1", features = ["zeroize"] }
rand = "0.8"

View File

@@ -0,0 +1,31 @@
use base64::Engine;
use base64::engine::general_purpose::STANDARD;
use ml_kem::kem::Encapsulate;
use ml_kem::{Encoded, EncodedSizeUser, KemCore, MlKem768};
fn main() {
let ek_bytes = STANDARD.decode("fZKrgJTGZYMogAO0u8XI/skLXXCx/hCmQjKdEGGaBitoBWCrbaw\
+t7nAT7G8lexlEdwGJrdwNuBVJFZOEnxH9Fu2e7kZrAA0EWZThrhrlCQb7SKSJ3MKP9jGeNA2igU/cXhQj9qSaWzNqplMffhuZHjBAT\
Nxglk6IwBcUnNpgEit0dYJgsxsMkfD8AzOaHKm4ybKwzJtYBN6nREl/OZE9CvDtVmZx+wkE1JwWuJJzLqdHWvJ4lyFq2YRUnxtpACKp\
xAkVfOzRuqlPUDJijwAc1UvXaFDLydJ/kcKiAorUfQtUoVJ8hkGK8OED3B15NxfJsRwt1kLhEgk89lD6GGhRRFmU8WQHBuX0pppe5A5\
BEm148Gv82c+KUUXSAcIFTFMg+UyPDKCaPFu5XBfyvM5RveXuyBFs/IF47BTmKfMVVOPXuh/SIZCONsBAOZgoneexKp8NyjMsVc8kWC\
OCVc+2sIcKjIElmRtjDFK9vooRVwGXppfPcV2vMeUk5IMF7aGnDdjkdGm1/oYkPqnvesi5bIpL+kGj9EPDibIPAS4AUQajgNNioRErB\
mxAdlZHWpXD+ixmbJp4geG7gJ3OsbBhmmBtJuPTcZeSLOg3HVtQTwyhxd1WjcMh1eqp0i5bDF/0Zin8dcqE0JRmTBj4MTAEbs1xNe1+\
xCZB3tnviVOU9d5uSpF6MNmdVWFiYmJJFYyeiJIOWmL1Och79S565dhxxO+ndd2DHVIuTaBW3cu4WSn9AlMqAcPvMxPAYcY7kFcOjc/\
PvYI4+oIfGgLaCcFNWcZUCBKz2Qi1zYqSjYYTrnJQVGClpOaGpdXi0zDYvagA2ZSDzNZS6oki6ZeimU0YzQDlRirNPx6ARSgdJU7WgA\
SpUNen0QLcoa7iwTDwGeEkHMt2OhUmqdv8bUNJ7YjpJuHOKuMX2C6fJslY5pyP5FJNVE7BugCSdSU1rxOKcHN0iiQSEgd8BU8hkZmof\
GahyzPgABI1cJjViMb/cgoiRtrsZuzBWDCVjog8mx7ulYhkszM9ghookGbHHwx/abBbRcTr1HIl0ursSCJPoZT3UWFRNgvVuA7gMQVJ\
FxxiFBOxOuHrMTABhVdpPCpNYvEgWRC+Skj3XhZRWovTLuDWPazRMe31iWVirBWTUIeIEEiRsvBPXMW3Ne1gsVwDFEOiaxp5SOAnAGK\
mkg/8LIRTHA0LbpBDEjEK+VllQeCIaUJk9g2FxlgatuqcDgIY2KH2NZapIOiRyZYs8shb2JJIRAWqcwRNIBL7ZwBOpafsyJc3BS4YSY\
ErVdAOOZe8/G9mcrNwOKzkkPFDUsb+eBEdHIr4bCUGGyinLkGskkXb1Gl8FCbTOGU04ZZtqhFI6eP5Stb7MiY7ywRhiOvokduQTLN26\
CsRJa579VOqdeSsNjFI6lxMhgwaxGaHcwWiGBDSbGGcyskujyPebYl+UlhDXGGVHYfZStkj8Sm84KGWqaq9BSQmIgU8oCj7SRMTGC3c\
dmjK5QiTGV+LEZKWwpQ9+GU5TIrTqImmeBj+dpGoPFmbliytiKbH53ZsFCfVNMqOvJ24IxdfgWNpBmrChnYmLQ=").unwrap();
let e: Encoded<<MlKem768 as KemCore>::EncapsulationKey> =
ek_bytes.as_slice().try_into().unwrap();
let ek = <MlKem768 as KemCore>::EncapsulationKey::from_bytes(&e);
let mut rng = rand::rngs::OsRng;
let k = ek.encapsulate(&mut rng).unwrap();
println!("{}", STANDARD.encode(k.0.as_slice()));
println!("Shared secret: {}", STANDARD.encode(k.1.as_slice()));
}

257
__crypto/okta-bcrypt-issue/Cargo.lock generated Normal file
View File

@@ -0,0 +1,257 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "base64"
version = "0.22.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"
[[package]]
name = "bcrypt"
version = "0.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "92758ad6077e4c76a6cadbce5005f666df70d4f13b19976b1a8062eef880040f"
dependencies = [
"base64",
"blowfish",
"getrandom",
"subtle",
"zeroize",
]
[[package]]
name = "bitflags"
version = "2.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967"
[[package]]
name = "blowfish"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e412e2cd0f2b2d93e02543ceae7917b3c70331573df19ee046bcbc35e45e87d7"
dependencies = [
"byteorder",
"cipher",
]
[[package]]
name = "byteorder"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
[[package]]
name = "cfg-if"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9555578bc9e57714c812a1f84e4fc5b4d21fcb063490c624de019f7464c91268"
[[package]]
name = "cipher"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad"
dependencies = [
"crypto-common",
"inout",
]
[[package]]
name = "crypto-common"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3"
dependencies = [
"generic-array",
"typenum",
]
[[package]]
name = "generic-array"
version = "0.14.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a"
dependencies = [
"typenum",
"version_check",
]
[[package]]
name = "getrandom"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26145e563e54f2cadc477553f1ec5ee650b00862f0a58bcd12cbdc5f0ea2d2f4"
dependencies = [
"cfg-if",
"libc",
"r-efi",
"wasi",
]
[[package]]
name = "inout"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "879f10e63c20629ecabbb64a8010319738c66a5cd0c29b02d63d272b03751d01"
dependencies = [
"generic-array",
]
[[package]]
name = "libc"
version = "0.2.174"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1171693293099992e19cddea4e8b849964e9846f4acee11b3948bcc337be8776"
[[package]]
name = "okta-bcrypt-issue"
version = "0.1.0"
dependencies = [
"base64",
"bcrypt",
"rand",
]
[[package]]
name = "ppv-lite86"
version = "0.2.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9"
dependencies = [
"zerocopy",
]
[[package]]
name = "proc-macro2"
version = "1.0.95"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d"
dependencies = [
"proc-macro2",
]
[[package]]
name = "r-efi"
version = "5.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f"
[[package]]
name = "rand"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9fbfd9d094a40bf3ae768db9361049ace4c0e04a4fd6b359518bd7b73a73dd97"
dependencies = [
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99d9a13982dcf210057a8a78572b2217b667c3beacbf3a0d8b454f6f82837d38"
dependencies = [
"getrandom",
]
[[package]]
name = "subtle"
version = "2.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292"
[[package]]
name = "syn"
version = "2.0.104"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17b6f705963418cdb9927482fa304bc562ece2fdd4f616084c50b7023b435a40"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "typenum"
version = "1.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1dccffe3ce07af9386bfd29e80c0ab1a8205a2fc34e4bcd40364df902cfa8f3f"
[[package]]
name = "unicode-ident"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"
[[package]]
name = "version_check"
version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
[[package]]
name = "wasi"
version = "0.14.2+wasi-0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9683f9a5a998d873c0d21fcbe3c083009670149a8fab228644b8bd36b2c48cb3"
dependencies = [
"wit-bindgen-rt",
]
[[package]]
name = "wit-bindgen-rt"
version = "0.39.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1"
dependencies = [
"bitflags",
]
[[package]]
name = "zerocopy"
version = "0.8.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1039dd0d3c310cf05de012d8a39ff557cb0d23087fd44cad61df08fc31907a2f"
dependencies = [
"zerocopy-derive",
]
[[package]]
name = "zerocopy-derive"
version = "0.8.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ecf5b4cc5364572d7f4c329661bcc82724222973f2cab6f050a4e5c22f75181"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "zeroize"
version = "1.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ced3678a2879b30306d323f4542626697a464a97c0a07c9aebf7ebca65cd4dde"

View File

@@ -0,0 +1,9 @@
[package]
name = "okta-bcrypt-issue"
version = "0.1.0"
edition = "2024"
[dependencies]
base64 = "0.22.1"
bcrypt = "0.17.0"
rand = "0.9.1"

View File

@@ -0,0 +1,33 @@
use rand::RngCore;
use base64::{Engine as _, engine::general_purpose::URL_SAFE};
use std::error::Error;
fn random_string(length: usize) -> String {
let mut bytes = vec![0u8; length];
rand::rng().fill_bytes(&mut bytes);
URL_SAFE.encode(&bytes)[..length].to_string()
}
// https://n0rdy.foo/posts/20250121/okta-bcrypt-lessons-for-better-apis/
fn main() -> Result<(), Box<dyn Error>> {
// 18 + 55 + 1 = 74, so above 72 characters' limit of BCrypt
let user_id = random_string(18);
let username = random_string(55);
let password = "super-duper-secure-password";
let combined_string = format!("{}:{}:{}", user_id, username, password);
let combined_hash = bcrypt::hash(combined_string.as_bytes(), bcrypt::DEFAULT_COST)?;
// let's try to break it
let wrong_password = "wrong-password";
let wrong_combined_string = format!("{}:{}:{}", user_id, username, wrong_password);
match bcrypt::verify(wrong_combined_string.as_bytes(), &combined_hash) {
Ok(true) => println!("Password is correct"),
Ok(false) => println!("Password is incorrect"),
Err(e) => println!("{}", e),
}
Ok(())
}

108
__crypto/orion-demo/Cargo.lock generated Normal file
View File

@@ -0,0 +1,108 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "bitflags"
version = "2.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967"
[[package]]
name = "cfg-if"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9555578bc9e57714c812a1f84e4fc5b4d21fcb063490c624de019f7464c91268"
[[package]]
name = "ct-codecs"
version = "1.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b10589d1a5e400d61f9f38f12f884cfd080ff345de8f17efda36fe0e4a02aa8"
[[package]]
name = "fiat-crypto"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "64cd1e32ddd350061ae6edb1b082d7c54915b5c672c389143b9a63403a109f24"
[[package]]
name = "getrandom"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26145e563e54f2cadc477553f1ec5ee650b00862f0a58bcd12cbdc5f0ea2d2f4"
dependencies = [
"cfg-if",
"libc",
"r-efi",
"wasi",
]
[[package]]
name = "hex"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
[[package]]
name = "libc"
version = "0.2.174"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1171693293099992e19cddea4e8b849964e9846f4acee11b3948bcc337be8776"
[[package]]
name = "orion"
version = "0.17.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "21b3da83b2b4cdc74ab6a556b2e7b473da046d5aa4008c0a7a3ae96b1b4aabb4"
dependencies = [
"ct-codecs",
"fiat-crypto",
"getrandom",
"subtle",
"zeroize",
]
[[package]]
name = "orion-demo"
version = "0.1.0"
dependencies = [
"hex",
"orion",
]
[[package]]
name = "r-efi"
version = "5.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f"
[[package]]
name = "subtle"
version = "2.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292"
[[package]]
name = "wasi"
version = "0.14.2+wasi-0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9683f9a5a998d873c0d21fcbe3c083009670149a8fab228644b8bd36b2c48cb3"
dependencies = [
"wit-bindgen-rt",
]
[[package]]
name = "wit-bindgen-rt"
version = "0.39.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1"
dependencies = [
"bitflags",
]
[[package]]
name = "zeroize"
version = "1.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ced3678a2879b30306d323f4542626697a464a97c0a07c9aebf7ebca65cd4dde"

Some files were not shown because too many files have changed in this diff Show More