← Back to team overview

dhis2-devs team mailing list archive

Re: Automatically mass-generate dhis2 usernames and passwords

 

Again, look at the R code. I tried to do this in pure R, but it didn't
work. Therefore, I call the Java class directly to calculate the hashCode.
I suppose one could delve into the depths of this part of the Java source
code, but I did not go there. Easier just to call the Java class directly
and not try and remake the wheel.

Regards,
Jason



On Fri, Jul 5, 2013 at 6:10 PM, Bob Jolliffe <bobjolliffe@xxxxxxxxx> wrote:

> Ah Jason I've been down this path (in tcl not surprisingly) ...
>
> You are almost correct.  For short strings.  But there is some conditional
> related to the length of the string.  Don't remember the details.  I think
> the string hash code is run over a truncated string.  I'll leave that to
> someone more enthusiastic.
>
> I mention VBA just because I guess excel would be a common environment for
> creating these masses of users.  At least they did that in Rwanda.  Mind
> you there is a horrible temptation to create these with simple and/or
> duplicate passwords.
>
>
> On 5 July 2013 16:46, Jason Pickering <jason.p.pickering@xxxxxxxxx> wrote:
>
>> VBA? Really?
>>
>> It is pretty simple actually.. Look at the R code and it is
>> "supersimple".  First you calculate the Java hash code (
>> http://en.wikipedia.org/wiki/Java_hashCode()) of the user name and wrap
>> this in curly braces and prepend the desired password. . Since the
>> algorithm is well documented, I guess a creation of VBA function to do this
>> should be simple.  Concatenate the password and the hashed username
>> together, and calculate the MD5 sum of that string. What results is the
>> salted encrypted password which should work for DHIS2.
>>
>> But then one either needs to create a series of SQL inserts (userinfo,
>> users, usermembership, userrolemembers in that order) or I guess use DXF to
>> import it all. Have done it in R, but not sure I want to try with VBA.  :)
>>
>> Regards,
>> Jason
>>
>>
>>
>>
>>
>> On Fri, Jul 5, 2013 at 5:40 PM, Bob Jolliffe <bobjolliffe@xxxxxxxxx>wrote:
>>
>>> I suspect what would be really useful is to have a VBA (or whatever the
>>> .net equivalent is) of this hash algorithm so that it can be easily used
>>> with excel.
>>>
>>> I started picking the spring algorithm with java string hash apart once
>>> .. got about 80% through then got bored and stuck with java.
>>>
>>>
>>> On 5 July 2013 16:15, Jason Pickering <jason.p.pickering@xxxxxxxxx>wrote:
>>>
>>>> Thanks for this. Looks very useful. Another way to do this (which I
>>>> have mentioned before on the list, is with R). Here are a couple of  R code
>>>> fragments which I think I have sent previously.
>>>>
>>>> #Generate the Java Hash Code. R does not calculate this properly, so we
>>>> have to revert to using Java for now.
>>>> javaHashCode<-function(string="") {
>>>> require(rJava)
>>>> .jinit()
>>>> s <- .jnew("java/lang/String", string)
>>>> .jcall(s,"I","hashCode") }
>>>>
>>>> #Start to generate the encrypted passwords
>>>> genEncryptedPass<-function(password="district",username="admin") {
>>>> #This is needed for the digest function
>>>> require(digest)
>>>> digest(paste0(password,"{",javaHashCode(username),"}"),serialize=FALSE)
>>>> }
>>>>
>>>> #Start to build up the data frame here
>>>> result<-as.data.frame(cbind(usernames,passwords),stringsAsFactors=FALSE)
>>>>
>>>> result$encrypt<-mapply(genEncryptedPass,password=result$passwords,username=result$usernames)
>>>>
>>>> So, with these two basic functions (javaHashCode and genEncryptedPass),
>>>> you can generate the meat of what you need. If you have a list of usernames
>>>> and passwords (which of course can also be generated in R) in a dataframe
>>>> called "result", the last line will give you the encrypted password.
>>>>
>>>> Using SQL, you can then generate of course some insert statements or
>>>> DXF to import into DHIS2. If anyone needs more code for this, I can provide
>>>> some more details. I suspect the same thing could be easily done with other
>>>> scripting languages as well.
>>>>
>>>> I think these Java classes will help but would suggest to abstract out
>>>> these methods a bit as right now, the DB connection is hard coded?
>>>>
>>>> Regards,
>>>> Jason
>>>>
>>>>
>>>>
>>>> On Fri, Jul 5, 2013 at 4:26 PM, Ngoc Thanh Nguyen <
>>>> thanh.hispvietnam@xxxxxxxxx> wrote:
>>>>
>>>>> In case you need to expedite creation of thousands of users and
>>>>> passwords the following classes might help. In essence, all of user
>>>>> creation steps can be done by sql. However, password hash is a bit tricky
>>>>> in dhis2 when it depends on Spring Security.
>>>>>
>>>>>
>>>>> https://dl.dropboxusercontent.com/u/63702128/dhis2%20user%20generator/GeneratePassword.java
>>>>>
>>>>> https://dl.dropboxusercontent.com/u/63702128/dhis2%20user%20generator/RandomGenerator.java
>>>>>
>>>>> After having a list of username and password pairs you can use sql to
>>>>> fill in users and userinfo tables, plus other germane tables for assigning
>>>>> orgunit and roles.
>>>>>
>>>>> Thanh
>>>>>
>>>>> _______________________________________________
>>>>> Mailing list: https://launchpad.net/~dhis2-devs
>>>>> Post to     : dhis2-devs@xxxxxxxxxxxxxxxxxxx
>>>>> Unsubscribe : https://launchpad.net/~dhis2-devs
>>>>> More help   : https://help.launchpad.net/ListHelp
>>>>>
>>>>>
>>>>
>>>> _______________________________________________
>>>> Mailing list: https://launchpad.net/~dhis2-devs
>>>> Post to     : dhis2-devs@xxxxxxxxxxxxxxxxxxx
>>>> Unsubscribe : https://launchpad.net/~dhis2-devs
>>>> More help   : https://help.launchpad.net/ListHelp
>>>>
>>>>
>>>
>>
>

Follow ups

References