V8 field scrambling

From DreamFactory
Jump to: navigation, search
DreamFactoryTutorialsV8 field scrambling
(Created page with "Scramble social security numbers during development and testing. When a social security number is posted, "encrypt" (scramble the digits), then POST the randomized digits to t...")
 
Line 2: Line 2:
  
 
<source lang="javascript">
 
<source lang="javascript">
 +
var privateKey = 27114;
 +
 
// post.pre_process
 
// post.pre_process
  
Line 20: Line 22:
 
     }
 
     }
  
     record.ssn = encode(platform.session.user.id, record.ssn);
+
     record.ssn = encode(private_key, record.ssn);
  
 
});
 
});
Line 38: Line 40:
  
 
lodash._.each(event.response.content.resource, function (record) {
 
lodash._.each(event.response.content.resource, function (record) {
     record.ssn = decode(platform.session.user.id, record.ssn || '');
+
     record.ssn = decode(private_key, record.ssn || '');
 
});
 
});
 
</source>
 
</source>

Revision as of 18:20, 29 June 2016

Scramble social security numbers during development and testing. When a social security number is posted, "encrypt" (scramble the digits), then POST the randomized digits to the database. Then show how to GET the social security number by decrypting the scrambled digits with a private key and returning the real social security number back to the client. For this change to take effect you have to enable modification of request and response in admin console script editor. Checkbox label is 'Allow script to modify request (pre-process) or response (post-process)'.

var privateKey = 27114;
 
// post.pre_process
 
function encode(key, value) {
    var result = "";
    for (i = 0; i < value.length; ++i) {
        result += String.fromCharCode(value.charCodeAt(i) + parseInt(key));
    }
    return result;
};
 
var lodash = require('lodash.min.js');
 
lodash._.each(event.request.payload.resource, function (record) {
 
    if (!record.ssn) {
        throw "Social Security Number is required!";
    }
 
    record.ssn = encode(private_key, record.ssn);
 
});
 
 
// get.pre_process
 
function decode(key, value) {
    var result = "";
    for (i = 0; i < value.length; ++i) {
        result += String.fromCharCode(value.charCodeAt(i) - parseInt(key));
    }
    return result;
};
 
var lodash = require('lodash.min.js');
 
lodash._.each(event.response.content.resource, function (record) {
    record.ssn = decode(private_key, record.ssn || '');
});