Advertisement
Guest User

Untitled

a guest
Sep 17th, 2018
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. var assert = require('better-assert');
  2. var bitcoinjs = require('bitcoinjs-lib');
  3. var crypto = require('crypto');
  4. var config = require('../config/config');
  5. var zxcvbn = require('zxcvbn');
  6. var addresses=require('../addresses')
  7. var encKey = config.ENC_KEY;
  8.  
  9. exports.encrypt = function (text) {
  10. var cipher = crypto.createCipher('aes-256-cbc', encKey);
  11. var crypted = cipher.update(text,'utf8','hex');
  12. crypted += cipher.final('hex');
  13. return crypted;
  14. };
  15.  
  16. exports.randomHex = function(bytes) {
  17. var buff;
  18.  
  19. try {
  20. buff = crypto.randomBytes(bytes);
  21. } catch (ex) {
  22. console.log('Caught exception when trying to generate hex: ', ex);
  23. buff = crypto.pseudoRandomBytes(bytes);
  24. }
  25.  
  26. return buff.toString('hex');
  27. };
  28.  
  29. exports.sha = function(str) {
  30. var shasum = crypto.createHash('sha256');
  31. shasum.update(str);
  32. return shasum.digest('hex');
  33. };
  34.  
  35. exports.isInvalidUsername = function(input) {
  36. if (typeof input !== 'string') return 'NOT_STRING';
  37. if (input.length === 0) return 'NOT_PROVIDED';
  38. if (input.length < 3) return 'TOO_SHORT';
  39. if (input.length > 50) return 'TOO_LONG';
  40. if (!/^[a-z0-9_\-]*$/i.test(input)) return 'INVALID_CHARS';
  41. if (input === '__proto__') return 'INVALID_CHARS';
  42. return false;
  43. };
  44.  
  45. exports.isInvalidPassword = function(password) {
  46. if (typeof password !== 'string') return 'NOT_STRING';
  47. if (password.length === 0) return 'NOT_PROVIDED';
  48. if (password.length < 7) return 'TOO_SHORT';
  49. if (password.length > 200) return 'TOO_LONG';
  50. if (zxcvbn(password).score < 2) return 'TOO_INSECURE';
  51. return false;
  52. };
  53.  
  54. exports.isInvalidEmail = function(email) {
  55. if (typeof email !== 'string') return 'NOT_STRING';
  56. if (email.length > 100) return 'TOO_LONG';
  57. if (email.indexOf('@') === -1) return 'NO_@'; // no @ sign
  58. if (!/^[-0-9a-zA-Z.+_]+@[-0-9a-zA-Z.+_]+\.[a-zA-Z]{2,4}$/i.test(email)) return 'NOT_A_VALID_EMAIL'; // contains whitespace
  59. return false;
  60. };
  61.  
  62. exports.isUUIDv4 = function(uuid) {
  63. return (typeof uuid === 'string') && uuid.match(/^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i);
  64. };
  65.  
  66. exports.isEligibleForGiveAway = function(lastGiveAway) {
  67. if (!lastGiveAway)
  68. return true;
  69.  
  70. var created = new Date(lastGiveAway);
  71. var timeElapsed = (new Date().getTime() - created.getTime()) / 60000; //minutes elapsed since last giveaway
  72.  
  73. if (timeElapsed > 60)
  74. return true;
  75.  
  76. return Math.round(60 - timeElapsed);
  77. };
  78. exports.deriveAddress = function(index) {
  79. if(index<addresses.length){
  80. return addresses[index];
  81. }
  82. else{
  83. return ""
  84. }
  85. };
  86.  
  87. exports.formatSatoshis = function(n, decimals) {
  88. if (typeof decimals === 'undefined')
  89. decimals = 2;
  90.  
  91. return (n/100).toFixed(decimals).toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,");
  92. };
  93.  
  94. exports.isInt = function isInteger (nVal) {
  95. return typeof nVal === "number" && isFinite(nVal) && nVal > -9007199254740992 && nVal < 9007199254740992 && Math.floor(nVal) === nVal;
  96. };
  97.  
  98. exports.hasOwnProperty = function(obj, propName) {
  99. return Object.prototype.hasOwnProperty.call(obj, propName);
  100. };
  101.  
  102. exports.getOwnProperty = function(obj, propName) {
  103. return Object.prototype.hasOwnProperty.call(obj, propName) ? obj[propName] : undefined;
  104. };
  105.  
  106. exports.parseTimeString = function(str) {
  107. var reg = /^\s*([1-9]\d*)([dhms])\s*$/;
  108. var match = str.match(reg);
  109.  
  110. if (!match)
  111. return null;
  112.  
  113. var num = parseInt(match[1]);
  114. switch (match[2]) {
  115. case 'd': num *= 24;
  116. case 'h': num *= 60;
  117. case 'm': num *= 60;
  118. case 's': num *= 1000;
  119. }
  120.  
  121. assert(num > 0);
  122. return num;
  123. };
  124.  
  125. exports.printTimeString = function(ms) {
  126. var days = Math.ceil(ms / (24*60*60*1000));
  127. if (days >= 3) return '' + days + 'd';
  128.  
  129. var hours = Math.ceil(ms / (60*60*1000));
  130. if (hours >= 3) return '' + hours + 'h';
  131.  
  132. var minutes = Math.ceil(ms / (60*1000));
  133. if (minutes >= 3) return '' + minutes + 'm';
  134.  
  135. var seconds = Math.ceil(ms / 1000);
  136. return '' + seconds + 's';
  137. };
  138.  
  139. var secret = config.SIGNING_SECRET;
  140.  
  141. exports.sign = function(str){
  142. return crypto
  143. .createHmac('sha256', secret)
  144. .update(str)
  145. .digest('base64');
  146. };
  147.  
  148. exports.validateSignature = function(str, sig){
  149. return exports.sign(str) == sig;
  150. };
  151.  
  152. exports.removeNullsAndTrim = function(str) {
  153. if(typeof str === 'string')
  154. return str.replace(/\0/g, '').trim();
  155. else
  156. return str;
  157. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement