Friday, 29 December 2017

link for encryption example

https://gist.github.com/raytung/f7dc78bb4310d02217111246da8cfdb3




/*
 * AWS Sdk KMS spike: (assuming node v6.6+)
 * 1 - Create master key at KMS
 * 2 - Copy alias or ARN
 * 3 - run this i.e.
 * $ node spike.js KEY_ALIAS YOUR_PLAINTEXT_TO_ENCRYPT
 */
const AWS = require('aws-sdk');

// aws-sdk is not reading my region info so i'll have to set it here
// maybe you have it working properly
// aws-sdk reads in your aws credentials from ~/.aws/credentials
AWS.config.update({ region:'ap-southeast-2' });

const kms = new AWS.KMS();

// your input args
const KeyId = process.argv[2];
const Plaintext = process.argv[3];

// http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/KMS.html#encrypt-property
// @params KeyId String
// @params Plaintext String | Buffer
// @params EncryptionContext object (optional) http://docs.aws.amazon.com/kms/latest/developerguide/encryption-context.html
// @params GrantTokens [Strings] (optional) http://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#grant
const params = {
 KeyId: keyId, // your key alias or full ARN key
 Plaintext: secret, // your super secret.
};

kms.encrypt(params).promise().then(data => {
 const base64EncryptedString = data.CiphertextBlob.toString('base64');
 console.log('base64 encrypted string: ' + base64EncryptedString);
 return base64EncryptedString;
})
.then(base64EncryptedString => {
 // http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/KMS.html#decrypt-property
 // @params KeyId String
 // @params CiphertextBlob Buffer(base64)
 // @params EncryptionContext object (optional) http://docs.aws.amazon.com/kms/latest/developerguide/encryption-context.html
 // @params GrantTokens [Strings] (optional) http://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#grant
 return kms.decrypt({
  CiphertextBlob: Buffer(base64EncryptedString, 'base64')
 }).promise();
})
.then(data => {
 console.log('Your super secret is: ' + data.Plaintext.toString('ascii'));
 // do something with it
})
.catch(err => console.log(err, err.stack));

Thursday, 21 December 2017

lodash orderby example

var questions = [
    { number: '1', text: 'question_1','rank':5,company:"lives"},
    { number: '2', text: 'question_2','rank':4,company:"acme"},
    { number: '3', text: 'question_3','rank':6,company:"sparky"},
];


questions=_.orderBy(questions, ['company'],['asc']);
 obj = {print: questions};

cloud search suggest example.


http://docs.aws.amazon.com/cloudsearch/latest/developerguide/getting-suggestions.html#configuring-suggesters

-----------------------------------------------------------------------------
http://search-imdb-hd6ebyouhw2lczkueyuqksnuzu.us-west-2.cloudsearch.amazonaws.com/2013-01-01/suggest -d"q=oce&suggester=suggest_title" {"status":{"rid":"646f5s0oDAr8pVk=","time-ms":2}, "suggest":{ "query":"oce", "found":3, "suggestions":[ {"suggestion":"Ocean's Eleven","score":0,"id":"tt0054135"}, {"suggestion":"Ocean's Thirteen","score":0,"id":"tt0496806"}, {"suggestion":"Ocean's Twelve","score":0,"id":"tt0349903"} ] } }






-----------------------------------------------------------------------------------

Submitting Suggest Requests in Amazon CloudSearch

You submit suggest requests via HTTP GET to your domain's search endpoint at 2013-01-01/suggest. For information about controlling access to the suggest service, see configure access policies.
You must specify the API version in all suggest requests and that version must match the API version specified when the domain was created.
For example, the following request gets suggestions from the search-movies-rr2f34ofg56xneuemujamut52i.us-east-1.cloudsearch.amazonaws.com domain for the query string oce using the suggester called title.
---------------------------------------------------------------------------------
let suggest={"status":{"rid":"646f5s0oDAr8pVk=","time-ms":2},
 "suggest":{
   "query":"oce",
   "found":3,
   "suggestions":[
     {"suggestion":"Ocean's Eleven","score":0,"id":"tt0054135"},
     {"suggestion":"Ocean's Thirteen","score":0,"id":"tt0496806"},
     {"suggestion":"Ocean's Twelve","score":0,"id":"tt0349903"}
   ]
 }
}
undefined
suggest
{status: {…}, suggest: {…}}status: {rid: "646f5s0oDAr8pVk=", time-ms: 2}suggest: {query: "oce", found: 3, suggestions: Array(3)}__proto__: Object
suggest.suggest.suggestions
(3) [{…}, {…}, {…}]0: {suggestion: "Ocean's Eleven", score: 0, id: "tt0054135"}1: {suggestion: "Ocean's Thirteen", score: 0, id: "tt0496806"}2: {suggestion: "Ocean's Twelve", score: 0, id: "tt0349903"}length: 3__proto__: Array(0)
suggest.suggest.suggestions[0]
{suggestion: "Ocean's Eleven", score: 0, id: "tt0054135"}
suggest.suggest.suggestions[0].suggestion
"Ocean's Eleven"