Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
RandomPassword.cs
1using System;
2using Waher.Content;
3using Waher.Script;
9
11{
16 {
24 : base(Start, Length, Expression)
25 {
26 }
27
31 public override string FunctionName => nameof(RandomPassword);
32
37 public override bool IsAsynchronous => false;
38
46 public static string CreateRandomPassword(int NrBytes, int NrBuckets)
47 {
48 if (NrBytes < 16)
49 throw new ArgumentOutOfRangeException(nameof(NrBytes), "Must be at least 16.");
50
51 if (NrBuckets < 0)
52 throw new ArgumentOutOfRangeException(nameof(NrBuckets), "Must be non-negative.");
53
54 byte[] Proposal;
55 double[] H;
56 double MinH;
57
58 do
59 {
60 Proposal = Gateway.NextBytes(NrBytes);
61
62 if (NrBuckets > 1)
63 {
64 H = Histogram.Compute(Proposal, NrBuckets, 0, 256);
65 MinH = Min.CalcMin(H, null);
66 }
67 else MinH = 1;
68 }
69 while (MinH == 0);
70
71 return Base64Url.Encode(Proposal);
72 }
73
78 public static string CreateRandomPassword()
79 {
80 return CreateRandomPassword(32, 12);
81
83 //
84 // This condition approximately removes 1 in 2 generated passwords,
85 // if NrBytes=32, NrBuckets=12, reducing the strength from 256 bits to 255 bits.
86 //
87 // In a test of 100'000 randomly generated passwords, 42'973 passed
88 // (~43%), 57027 failed (~57%).
89 //
90 // Sample script:
91 //
92 // NrPass:=0;
93 // NrFail:=0;
94 // foreach x in 1..100000 do
95 // (
96 // Bin:=Base64Decode(Base64Encode(Gateway.NextBytes(32)));
97 // H:=Histogram([foreach x in Bin : x],0,256,12);
98 // if (Min(H[1])>0) then NrPass++ else NrFail++
99 // );
100 // {"NrPass":NrPass,"NrFail":NrFail}
101 //
103 }
104
111 {
112 return new StringValue(CreateRandomPassword(32, 12));
113 }
114 }
115}
Static class that does BASE64URL encoding (using URL and filename safe alphabet), as defined in RFC46...
Definition: Base64Url.cs:11
static string Encode(byte[] Data)
Converts a binary block of data to a Base64URL-encoded string.
Definition: Base64Url.cs:48
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:126
static byte[] NextBytes(int NrBytes)
Generates an array of random bytes.
Definition: Gateway.cs:3534
override IElement Evaluate(Variables Variables)
Evaluates the function.
RandomPassword(int Start, int Length, Expression Expression)
Creates a random password.
override bool IsAsynchronous
If the node (or its decendants) include asynchronous evaluation. Asynchronous nodes should be evaluat...
static string CreateRandomPassword(int NrBytes, int NrBuckets)
Creates a random password.
static string CreateRandomPassword()
Creates a random password having approximately 255 bits of entropy.
Class managing a script expression.
Definition: Expression.cs:39
static double CalcMin(double[] Values, ScriptNode Node)
Returns the smallest value.
Definition: Min.cs:54
Base class for funcions of zero variables.
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
Computes a histogram from a set of data.
Definition: Histogram.cs:14
static double[] Compute(double[] Data, int N, double Min, double Max)
Computes a Histogram over an array of floating-point values.
Definition: Histogram.cs:87
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:20