Skip to content

Commit 30dc058

Browse files
committed
Add random(max) and random(min, max) API's
1 parent 39d5cb6 commit 30dc058

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/ECCX08.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,32 @@ String ECCX08Class::serialNumber()
9191
return result;
9292
}
9393

94+
long ECCX08Class::random(long max)
95+
{
96+
return random(0, max);
97+
}
98+
99+
long ECCX08Class::random(long min, long max)
100+
{
101+
if (min >= max)
102+
{
103+
return min;
104+
}
105+
106+
long diff = max - min;
107+
108+
long r;
109+
random((byte*)&r, sizeof(r));
110+
111+
if (r < 0) {
112+
r = -r;
113+
}
114+
115+
r = (r % diff);
116+
117+
return (r + min);
118+
}
119+
94120
int ECCX08Class::random(byte data[], size_t length)
95121
{
96122
if (!wakeup()) {

src/ECCX08.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class ECCX08Class
3434

3535
String serialNumber();
3636

37+
long random(long max);
38+
long random(long min, long max);
3739
int random(byte data[], size_t length);
3840

3941
int generatePrivateKey(int slot, byte publicKey[]);

0 commit comments

Comments
 (0)