Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/cli/src/tunnels/challenge.rs
3314 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
#[cfg(not(feature = "vsda"))]
7
pub fn create_challenge() -> String {
8
use rand::distributions::{Alphanumeric, DistString};
9
Alphanumeric.sample_string(&mut rand::thread_rng(), 16)
10
}
11
12
#[cfg(not(feature = "vsda"))]
13
pub fn sign_challenge(challenge: &str) -> String {
14
use base64::{engine::general_purpose as b64, Engine as _};
15
use sha2::{Digest, Sha256};
16
let mut hash = Sha256::new();
17
hash.update(challenge.as_bytes());
18
let result = hash.finalize();
19
b64::URL_SAFE_NO_PAD.encode(result)
20
}
21
22
#[cfg(not(feature = "vsda"))]
23
pub fn verify_challenge(challenge: &str, response: &str) -> bool {
24
sign_challenge(challenge) == response
25
}
26
27
#[cfg(feature = "vsda")]
28
pub fn create_challenge() -> String {
29
use rand::distributions::{Alphanumeric, DistString};
30
let str = Alphanumeric.sample_string(&mut rand::thread_rng(), 16);
31
vsda::create_new_message(&str)
32
}
33
34
#[cfg(feature = "vsda")]
35
pub fn sign_challenge(challenge: &str) -> String {
36
vsda::sign(challenge)
37
}
38
39
#[cfg(feature = "vsda")]
40
pub fn verify_challenge(challenge: &str, response: &str) -> bool {
41
vsda::validate(challenge, response)
42
}
43
44