Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Hashing based on variables

  • matthewpulis
  • matthewpulis's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
2 years 3 months ago #234184 by matthewpulis
Hashing based on variables was created by matthewpulis
Please help us help you and fill where relevant:
Your LimeSurvey version: 3.28.7 
Own server or LimeSurvey hosting: own
Survey theme/template: lime
==================

I would like to label each record in my survey with an md5 (or otherwise) hashed variable. This is a pre and post experiment survey, so I would like to see the improvement after the experiment, without off course knowing who said what. I do not have access to the participants to give them a pre-generated token, so that is out of the question.

Thus, I would like to hash depending on the date of birth and the last 3 digits of the mobile number. The data recorded is not super sensitive, so these two variables as a hashing salt should be enough, and were judged by Ethics committee as enough.

Can someone please help me how to do it best? It seems LS does not have an md5() function? Ideally both dob and mob are not stored but just used on the fly.

I am attaching where I'm at at the moment, but the whole javascript for HashCode is not working at all (no alerts coming up).

The JS I'm using is this:
Code:
<script src="https://cdn.jsdelivr.net/npm/md5-js-tools@1.0.2/lib/md5.min.js"></script>
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
  alert ("test");
  var dob = {DateOfBirth.NAOK};
  alert (dob);
  var mob = {MobNo.NAOK};
  alert (mob);
  var s = dob + mob;
  alert (s);
  var hash = MD5.generate(s);
  alert (hash);
  var thisQuestion = $('#question{QID}');
  thisQuestion.val = hash;
  });
</script>

Any help would be extremely appreciated.

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 3 months ago #234201 by Joffm
Replied by Joffm on topic Hashing based on variables
Hi,
1. I think you have to convert your date into a string, like
var dob = {date('dmY',strtotime(DateOfBirth.NAOK))};

2. You have to select the place where you show your result
$('input[type=text]', thisQuestion).val(hash);
not only $('thisQuestion).val(hash);

3. I used a different library
[url] github.com/blueimp/JavaScript-MD5 [/url]

Now I get
 

Joffm

 

Volunteers are not paid.
Not because they are worthless, but because they are priceless

Please Log in to join the conversation.

  • matthewpulis
  • matthewpulis's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
2 years 3 months ago #234210 by matthewpulis
Replied by matthewpulis on topic Hashing based on variables
Thanks Joffm.

We're getting there :) However, it seems I am having a problem with any library I use:
Code:
<script src="https://raw.githubusercontent.com/blueimp/JavaScript-MD5/master/js/md5.min.js"></script><script type="text/javascript" charset="utf-8">
$(document).ready(function() {
  alert ("test");
  var dob = {date('dmY',strtotime(DateOfBirth.NAOK))};
  alert (dob);
  var mob = {MobNo.NAOK};
  alert (mob);
  var s = dob * mob;
  var ss = s.toString();
  var smob = mob.toString();
  var sss = ss +  '_' + smob;
  alert (sss);
  var hash2 = md5('value');
  alert (hash2);
  var hash = md5(sss);
  alert (hash);
  $('input[type=text]', thisQuestion).val(hash);
  });
</script>

alert (sss) works fine... but seems hash2 is not being filled .. since the following alerts() are not firing. Any ideas why might be stopping the inclusion of the external libraries, please?

In other news, is it possible to not save the variables passed in the previous question group, please?

Thanks a lot!

Please Log in to join the conversation.

  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 3 months ago #234212 by holch
Replied by holch on topic Hashing based on variables

Any ideas why might be stopping the inclusion of the external libraries, please?


Difficult to say why external libraries don't work on your server.

Is your server behind some firewall / proxy for example?

In other news, is it possible to not save the variables passed in the previous question group, please?


Just use a hidden equation type question and attribute a different value to the question by a code like this:

[code]{questioncode="###"}[/quote]

This will reset the answer given. But you need to make sure, that they has is already written and saved in a hidden question somewhere before you overwrite the responses in the questions you use to create the hash.

Help us to help you!
  • Provide your LS version and where it is installed (own server, uni/employer, SaaS hosting, etc.).
  • Always provide a LSS file (not LSQ or LSG).
Note: I answer at this forum in my spare time, I'm not a LimeSurvey GmbH employee.

Please Log in to join the conversation.

  • matthewpulis
  • matthewpulis's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
2 years 3 months ago - 2 years 3 months ago #234214 by matthewpulis
Replied by matthewpulis on topic Hashing based on variables
holch, so to make sure I'm understanding you:

question group 1: get the variables which I'll hash
question group 2:
- 2 variables:
1) text & hidden: do the javascript magic (as above)
2) equation & hidden: hide the variables in qs group 1.

correct?

re: external scripts, nothing that I'm aware of. Is there an easy way I can test if the library is being imported, please? and I don't believe I have any syntax errors in my code, right?
Last edit: 2 years 3 months ago by matthewpulis.

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 3 months ago #234215 by Joffm
Replied by Joffm on topic Hashing based on variables
Hi, I do not see an issue with this code (when I store the library on my serverI)
Code:
<script src="/lime3/upload/surveys/{SID}/files/md5.js"></script>
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
  var dob = {date('dmY',strtotime(DateOfBirth.NAOK))};
  var mob = {MobNo.NAOK};
  var sss = dob * mob +  '_' + mob;
  var hash = md5(sss);
  var thisQuestion = $('#question{QID}');
  $('input[type=text]', thisQuestion).val(hash);
  });
</script>

But with this I do not get a result
Code:
<script src="https://raw.githubusercontent.com/blueimp/JavaScript-MD5/master/js/md5.js"></script>
  




I don't know why.

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless

Please Log in to join the conversation.

  • matthewpulis
  • matthewpulis's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
2 years 3 months ago #234218 by matthewpulis
Replied by matthewpulis on topic Hashing based on variables
So I'll store it locally then :) Thanks will try later on and report back.

Please Log in to join the conversation.

  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 3 months ago #234219 by holch
Replied by holch on topic Hashing based on variables

holch, so to make sure I'm understanding you:

question group 1: get the variables which I'll hash
question group 2:
- 2 variables:
1) text & hidden: do the javascript magic (as above)
2) equation & hidden: hide the variables in qs group 1.

correct?

re: external scripts, nothing that I'm aware of. Is there an easy way I can test if the library is being imported, please? and I don't believe I have any syntax errors in my code, right?
Step 2 is not "hiding the variables". It is overwriting them. 

From what I understand, you have two questions, "DateOfBirth" and "MobNo".

So let's assume in "DateOfBirth" you have stored something like "25/11/2022" and in "MobNo" something like "+552198765432".

Now you calculate your hash and store it somewhere.

From what I understand, you want to erase the birth date and the Mobile Number from the database.

So in a later "page" (e.g. the last page of the questionnaire") you'll put something like this in an equation:
Code:
DateOfBirth="erased"
MobNo="erased"

Now you will have only the string "erased" stored in those to questions.

Help us to help you!
  • Provide your LS version and where it is installed (own server, uni/employer, SaaS hosting, etc.).
  • Always provide a LSS file (not LSQ or LSG).
Note: I answer at this forum in my spare time, I'm not a LimeSurvey GmbH employee.

Please Log in to join the conversation.

Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose