Fossil SCM

Improvements to the test-random-password command.

drh 2021-01-02 12:58 trunk
Commit 931b97e8ae1113b6331a6d103685daa673b15f1f4acc0b9581fa2a0b91703731
1 file changed +34 -8
+34 -8
--- src/util.c
+++ src/util.c
@@ -21,10 +21,11 @@
2121
#include "util.h"
2222
#if defined(USE_MMAN_H)
2323
# include <sys/mman.h>
2424
# include <unistd.h>
2525
#endif
26
+#include <math.h>
2627
2728
/*
2829
** For the fossil_timer_xxx() family of functions...
2930
*/
3031
#ifdef _WIN32
@@ -697,12 +698,12 @@
697698
/* 0 1 2 3 4 5 */
698699
/* 123456789 123456789 123456789 123456789 123456789 123456 */
699700
"23456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
700701
701702
if( N<8 ) N = 8;
702
- else if( N>sizeof(zAlphabet)-2 ) N = sizeof(zAlphabet)-2;
703703
nSrc = sizeof(zAlphabet) - 1;
704
+ if( N>nSrc ) N = nSrc;
704705
memcpy(zSrc, zAlphabet, nSrc);
705706
706707
for(i=0; i<N; i++){
707708
unsigned r;
708709
sqlite3_randomness(sizeof(r), &r);
@@ -715,22 +716,47 @@
715716
}
716717
717718
/*
718719
** COMMAND: test-random-password
719720
**
720
-** Usage: %fossil test-random-password ?N?
721
+** Usage: %fossil test-random-password [N] [--entropy]
721722
**
722723
** Generate a random password string of approximately N characters in length.
723
-** If N is omitted, use 10. Values of N less than 8 are changed to 8
724
-** and greater than 55 and changed to 55.
724
+** If N is omitted, use 12. Values of N less than 8 are changed to 8
725
+** and greater than 57 and changed to 57.
726
+**
727
+** If the --entropy flag is included, the number of bits of entropy in
728
+** the password is show as well.
725729
*/
726730
void test_random_password(void){
727
- int N = 10;
728
- if( g.argc>=3 ){
729
- N = atoi(g.argv[2]);
731
+ int N = 12;
732
+ int showEntropy = 0;
733
+ int i;
734
+ char *zPassword;
735
+ for(i=2; i<g.argc; i++){
736
+ const char *z = g.argv[i];
737
+ if( z[0]=='-' && z[1]=='-' ) z++;
738
+ if( strcmp(z,"-entropy")==0 ){
739
+ showEntropy = 1;
740
+ }else if( fossil_isdigit(z[0]) ){
741
+ N = atoi(z);
742
+ if( N<8 ) N = 8;
743
+ if( N>57 ) N = 57;
744
+ }else{
745
+ usage("[N] [--entropy]");
746
+ }
747
+ }
748
+ zPassword = fossil_random_password(N);
749
+ if( showEntropy ){
750
+ double et = 57.0;
751
+ for(i=1; i<N; i++) et *= 57-i;
752
+ fossil_print("%s (%d bits of entropy)\n", zPassword,
753
+ (int)(log(et)/log(2.0)));
754
+ }else{
755
+ fossil_print("%s\n", zPassword);
730756
}
731
- fossil_print("%s\n", fossil_random_password(N));
757
+ fossil_free(zPassword);
732758
}
733759
734760
/*
735761
** Return the number of decimal digits in a nonnegative integer. This is useful
736762
** when formatting text.
737763
--- src/util.c
+++ src/util.c
@@ -21,10 +21,11 @@
21 #include "util.h"
22 #if defined(USE_MMAN_H)
23 # include <sys/mman.h>
24 # include <unistd.h>
25 #endif
 
26
27 /*
28 ** For the fossil_timer_xxx() family of functions...
29 */
30 #ifdef _WIN32
@@ -697,12 +698,12 @@
697 /* 0 1 2 3 4 5 */
698 /* 123456789 123456789 123456789 123456789 123456789 123456 */
699 "23456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
700
701 if( N<8 ) N = 8;
702 else if( N>sizeof(zAlphabet)-2 ) N = sizeof(zAlphabet)-2;
703 nSrc = sizeof(zAlphabet) - 1;
 
704 memcpy(zSrc, zAlphabet, nSrc);
705
706 for(i=0; i<N; i++){
707 unsigned r;
708 sqlite3_randomness(sizeof(r), &r);
@@ -715,22 +716,47 @@
715 }
716
717 /*
718 ** COMMAND: test-random-password
719 **
720 ** Usage: %fossil test-random-password ?N?
721 **
722 ** Generate a random password string of approximately N characters in length.
723 ** If N is omitted, use 10. Values of N less than 8 are changed to 8
724 ** and greater than 55 and changed to 55.
 
 
 
725 */
726 void test_random_password(void){
727 int N = 10;
728 if( g.argc>=3 ){
729 N = atoi(g.argv[2]);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
730 }
731 fossil_print("%s\n", fossil_random_password(N));
732 }
733
734 /*
735 ** Return the number of decimal digits in a nonnegative integer. This is useful
736 ** when formatting text.
737
--- src/util.c
+++ src/util.c
@@ -21,10 +21,11 @@
21 #include "util.h"
22 #if defined(USE_MMAN_H)
23 # include <sys/mman.h>
24 # include <unistd.h>
25 #endif
26 #include <math.h>
27
28 /*
29 ** For the fossil_timer_xxx() family of functions...
30 */
31 #ifdef _WIN32
@@ -697,12 +698,12 @@
698 /* 0 1 2 3 4 5 */
699 /* 123456789 123456789 123456789 123456789 123456789 123456 */
700 "23456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
701
702 if( N<8 ) N = 8;
 
703 nSrc = sizeof(zAlphabet) - 1;
704 if( N>nSrc ) N = nSrc;
705 memcpy(zSrc, zAlphabet, nSrc);
706
707 for(i=0; i<N; i++){
708 unsigned r;
709 sqlite3_randomness(sizeof(r), &r);
@@ -715,22 +716,47 @@
716 }
717
718 /*
719 ** COMMAND: test-random-password
720 **
721 ** Usage: %fossil test-random-password [N] [--entropy]
722 **
723 ** Generate a random password string of approximately N characters in length.
724 ** If N is omitted, use 12. Values of N less than 8 are changed to 8
725 ** and greater than 57 and changed to 57.
726 **
727 ** If the --entropy flag is included, the number of bits of entropy in
728 ** the password is show as well.
729 */
730 void test_random_password(void){
731 int N = 12;
732 int showEntropy = 0;
733 int i;
734 char *zPassword;
735 for(i=2; i<g.argc; i++){
736 const char *z = g.argv[i];
737 if( z[0]=='-' && z[1]=='-' ) z++;
738 if( strcmp(z,"-entropy")==0 ){
739 showEntropy = 1;
740 }else if( fossil_isdigit(z[0]) ){
741 N = atoi(z);
742 if( N<8 ) N = 8;
743 if( N>57 ) N = 57;
744 }else{
745 usage("[N] [--entropy]");
746 }
747 }
748 zPassword = fossil_random_password(N);
749 if( showEntropy ){
750 double et = 57.0;
751 for(i=1; i<N; i++) et *= 57-i;
752 fossil_print("%s (%d bits of entropy)\n", zPassword,
753 (int)(log(et)/log(2.0)));
754 }else{
755 fossil_print("%s\n", zPassword);
756 }
757 fossil_free(zPassword);
758 }
759
760 /*
761 ** Return the number of decimal digits in a nonnegative integer. This is useful
762 ** when formatting text.
763

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button