static void enc (unsigned long v, char* b) {
int i;
-// int n = RChar.size ();
int n = SaltChar.size ();
for (i = 0; i < 5; i ++) {
-// b[i] = RChar[v % n];
- b[i] = SaltChar[v % n];
+ *b ++ = SaltChar[v % n];
v /= n;
}
}
return ustring (b, 12);
}
+ustring makeSalt (char digit, size_t len) {
+ char b[48];
+
+ if (len > 32)
+ len = 32;
+ if (! Inited)
+ init ();
+ b[0] = '$';
+ b[1] = digit;
+ b[2] = '$';
+ for (int i = 0; i < 35; i += 5) {
+ if (len > i) {
+ enc (random (), b + i + 3);
+ } else {
+ break;
+ }
+ }
+ b[len + 3] = '$';
+ return ustring (b, len + 4);
+}
+
double randDouble () {
if (! Inited)
init ();
ustring randomKey (unsigned long n);
ustring smallRandomKey ();
ustring makeSalt ();
+ustring makeSalt (char digit, size_t len);
double randDouble ();
long random_int (unsigned long n);
return (strcmp (crypt (pass.c_str (), cpass.c_str ()), cpass.c_str ()) == 0);
}
-ustring passCrypt (const ustring& pass) {
- ustring salt = makeSalt ();
+ustring passCrypt (const ustring& pass, passCryptFormat format) {
+ // XXX not thread safe.
+ ustring salt;
+ switch (format) {
+ case FORMAT_MD5:
+ salt = makeSalt ('1', 8);
+ break;
+// case FORMAT_BF:
+// salt = makeSalt ('2', 16);
+// break;
+ case FORMAT_SHA256:
+ salt = makeSalt ('5', 16);
+ break;
+ case FORMAT_SHA512:
+ salt = makeSalt ('6', 16);
+ break;
+ default:
+ assert (0);
+ }
+ std::cerr << salt << ":" << salt.length () << "\n";
return ustring (crypt (pass.c_str (), salt.c_str ()));
}
double strtod (const ustring& str);
long strtol (const ustring& str);
bool passMatch (const ustring& pass, const ustring& cpass);
-ustring passCrypt (const ustring& pass);
+typedef enum {
+ FORMAT_MD5,
+ FORMAT_BF,
+ FORMAT_SHA256,
+ FORMAT_SHA512,
+} passCryptFormat;
+ustring passCrypt (const ustring& pass, passCryptFormat format);
size_t strLength (const ustring& src);
void substring (const ustring& src, size_t idx, size_t len, int flen, ustring& ans);
ustring jsEncode (const ustring& str);
/*DOC:
===password-crypt===
- (password-crypt PASSWORD) -> STRING
+ (password-crypt PASSWORD [#md5 | #sha256 | #sha512]) -> STRING
+
+deprecated.
*/
//#AFUNC password-crypt ml_password_crypt
MNode* ml_password_crypt (MNode* cell, MlEnv* mlenv) {
MNode* arg = cell->cdr ();
ustring pass;
+ passCryptFormat format;
+ std::vector<MNode*> params;
+ std::vector<MNode*> keywords;
+ static paramList kwlist[] = {
+ {CharConst ("md5"), true},
+ {CharConst ("sha256"), true},
+ {CharConst ("sha512"), true},
+// {CharConst ("bf"), true},
+ {NULL, 0, 0}
+ };
- if (! arg)
- throw (uErrorWrongNumber);
- pass = eval_str (arg->car (), mlenv);
- nextNode (arg);
- if (arg)
- throw (uErrorWrongNumber);
+ format = FORMAT_MD5;
+ setParams (arg, 1, ¶ms, kwlist, &keywords, NULL);
+ pass = eval_str (params[0], mlenv);
+ if (keywords[0] && eval_bool (keywords[0], mlenv))
+ format = FORMAT_MD5;
+ if (keywords[1] && eval_bool (keywords[1], mlenv))
+ format = FORMAT_SHA256;
+ if (keywords[2] && eval_bool (keywords[2], mlenv))
+ format = FORMAT_SHA512;
+// if (keywords[3] && eval_bool (keywords[3], mlenv))
+// format = FORMAT_BF;
- return newMNode_str (new ustring (passCrypt (pass)));
+ return newMNode_str (new ustring (passCrypt (pass, format)));
}
/*DOC: