BVB Source Codes

parse-server Show cryptoUtils.js Source code

Return Download parse-server: download cryptoUtils.js Source code - Download parse-server Source code - Type:.js
  1. /* @flow */
  2.  
  3. import { randomBytes, createHash } from 'crypto';
  4.  
  5. // Returns a new random hex string of the given even size.
  6. export function randomHexString(size: number): string {
  7.   if (size === 0) {
  8.     throw new Error('Zero-length randomHexString is useless.');
  9.   }
  10.   if (size % 2 !== 0) {
  11.     throw new Error('randomHexString size must be divisible by 2.')
  12.   }
  13.   return randomBytes(size / 2).toString('hex');
  14. }
  15.  
  16. // Returns a new random alphanumeric string of the given size.
  17. //
  18. // Note: to simplify implementation, the result has slight modulo bias,
  19. // because chars length of 62 doesn't divide the number of all bytes
  20. // (256) evenly. Such bias is acceptable for most cases when the output
  21. // length is long enough and doesn't need to be uniform.
  22. export function randomString(size: number): string {
  23.   if (size === 0) {
  24.     throw new Error('Zero-length randomString is useless.');
  25.   }
  26.   const chars = ('ABCDEFGHIJKLMNOPQRSTUVWXYZ' +
  27.                'abcdefghijklmnopqrstuvwxyz' +
  28.                '0123456789');
  29.   let objectId = '';
  30.   const bytes = randomBytes(size);
  31.   for (let i = 0; i < bytes.length; ++i) {
  32.     objectId += chars[bytes.readUInt8(i) % chars.length];
  33.   }
  34.   return objectId;
  35. }
  36.  
  37. // Returns a new random alphanumeric string suitable for object ID.
  38. export function newObjectId(): string {
  39.   //TODO: increase length to better protect against collisions.
  40.   return randomString(10);
  41. }
  42.  
  43. // Returns a new random hex string suitable for secure tokens.
  44. export function newToken(): string {
  45.   return randomHexString(32);
  46. }
  47.  
  48. export function md5Hash(string: string): string {
  49.   return createHash('md5').update(string).digest('hex');
  50. }
  51.  
downloadcryptoUtils.js Source code - Download parse-server Source code
Related Source Codes/Software:
react-boilerplate - 2017-06-07
webtorrent - Streaming torrent client for the web ... 2017-06-06
machine-learning-for-software-engineers - A complete daily plan for studying to become a mac... 2017-06-06
upterm - A terminal emulator for the 21st century. 2017-06-06
lottie-android - Render After Effects animations natively on Androi... 2017-06-07
AsyncDisplayKit - Smooth asynchronous user interfaces for iOS apps. ... 2017-06-07
ionicons - The premium icon font for Ionic ... 2017-06-07
storybook - 2017-06-07
prettier - Prettier is an opinionated JavaScript formatter. ... 2017-06-08
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top