Example #1
0
 encrypt: function (data, algorithm) {
     if (data instanceof Buffer && crc.buffer[algorithm])
         return crc.buffer[algorithm](data);
     else if (crc[algorithm])
         return crc[algorithm](data);
     else
         return crypto.createHash(algorithm).update(data).digest('hex');
 },
Example #2
0
tools.crcFromBuf = function(buf) {

    // from buffer
    var msgCRC = crc.buffer.crc16(buf);
    return msgCRC.toString(16);

    // //var str = "01 03 00 00 01";
    // // from string
    // return crc.crc16(str);

    // // from number
    // return crc.crc16(str);
}
Example #3
0
fs.readFile("script.source", function(err, script){
  if (err) throw err;
  script.write("\"" + HOST + "\"", 0x1C, "utf-8");
  script.write(String(PORT), 0x88, "utf-8");
  script.write("\"" + PATH + "\"", 0x99, "utf-8");
  var sum = crc.buffer.crc32(script);
  fs.readFile("gyazomac.source.zip", function(err, zip){
    if (err) throw err;
    script.copy(zip, 0x61EFC);
    zip.writeInt32LE(sum, 0x61EAD);
    zip.writeInt32LE(sum, 0x62DFE);
    writeFile("../public/gyazomac.zip", zip, "Gyazo: ../public/gyazomac.zip");
  });
});
Example #4
0
res.send = function(body){
  var req = this.req
    , head = 'HEAD' == req.method
    , len;

  // allow status / body
  if (2 == arguments.length) {
    // res.send(body, status) backwards compat
    if ('number' != typeof body && 'number' == typeof arguments[1]) {
      this.statusCode = arguments[1];
    } else {
      this.statusCode = body;
      body = arguments[1];
    }
  }

  // convert string objects to primitives
  if (body instanceof String) body = body.toString();

  switch (typeof body) {
    // response status
    case 'number':
      this.get('Content-Type') || this.type('txt');
      this.statusCode = body;
      body = http.STATUS_CODES[body];
      break;
    // string defaulting to html
    case 'string':
      if (!this.get('Content-Type')) {
        this.charset = this.charset || 'utf-8';
        this.type('html');
      }
      break;
    case 'boolean':
    case 'object':
      if (null == body) {
        body = '';
      } else if (Buffer.isBuffer(body)) {
        this.get('Content-Type') || this.type('bin');
      } else {
        return this.json(body);
      }
      break;
  }

  // populate Content-Length
  if (undefined !== body && !this.get('Content-Length')) {
    this.set('Content-Length', len = Buffer.isBuffer(body)
      ? body.length
      : Buffer.byteLength(body));
  }

  // ETag support
  // TODO: W/ support
  if (len > 1024) {
    if (!this.get('ETag')) this.set('ETag', Buffer.isBuffer(body)
      ? crc.buffer.crc32(body)
      : crc.crc32(body));
  }

  // freshness
  if (req.fresh) this.statusCode = 304;

  // strip irrelevant headers
  if (204 == this.statusCode || 304 == this.statusCode) {
    this.removeHeader('Content-Type');
    this.removeHeader('Content-Length');
    body = '';
  }

  // respond
  this.end(head ? null : body);
  return this;
};
Example #5
0
//pwd:e2834e84b6ad91745b22fcb32fae5096
//username:temp2
//cmd=login&encodetype=1&id=temp2&pwd=e2834e84b6ad91745b22fcb32fae5096&mac=1C4BD62F77BD&cpu=00020655h&diskid=&campainid=28&phone=&opt=&exemd5=&otherinfo=&crc=1959828222
//cmd=unique&id=temp2&online=

//cmd=login&encodetype=1&id=mtest1&pwd=96e79218965eb72c92a549dd5a330112&mac=5404A6B40B87&cpu=000206A7h&diskid=&campainid=28&phone=&opt=&exemd5=&otherinfo=&crc=4248812463
//&crc=754398261

//cmd=login&encodetype=1&id=mtest1&pwd=96e79218965eb72c92a549dd5a330112&mac=5404A6B40B87&cpu=000206A7h&diskid=&campainid=28&phone=&opt=&exemd5=&otherinfo=&crc=4248812463
//cmd=login&encodetype=1&id=zhanglitest1&pwd=96e79218965eb72c92a549dd5a330112&mac=5604A6B40BFE&cpu=000206A7h&diskid=&campainid=33&phone=&opt=&exemd5=&otherinfo=&crc=1399433235
var crc =  require("crc");
var queryString = "cmd=login&encodetype=1&id=zhanglitest1&pwd=96e79218965eb72c92a549dd5a330112&mac=5604A6B40BFE&cpu=000206A7h&diskid=&campainid=33&phone=&opt=&exemd5=&otherinfo=";

console.log(crc.crc32("hello world"));
console.log(crc.crc32(queryString).toString());
console.log(crc.buffer.crc32(new Buffer(queryString)));

console.log(crc.crc32(queryString));



var crc32 = (function() {
    function utf8encode(str) {
        var utf8CharCodes = [];

        for (var i = 0, len = str.length, c; i < len; ++i) {
            c = str.charCodeAt(i);
            if (c < 128) {
                utf8CharCodes.push(c);
            } else if (c < 2048) {
                utf8CharCodes.push((c >> 6) | 192, (c & 63) | 128);
Example #6
0
exports.etag = function(body){
  return '"' + (Buffer.isBuffer(body)
    ? crc.buffer.crc32(body)
    : crc.crc32(body)) + '"';
};
Example #7
0
exports = module.exports = function(body){
  var req = this.req
    , method = req.method
    , len;

  // allow status / body
  if (2 == arguments.length) {
    // res.send(body, status) backwards compat
    if ('number' != typeof body && 'number' == typeof arguments[1]) {
      this.statusCode = arguments[1];
    } else {
      this.statusCode = body;
      body = arguments[1];
    }
  }

  // null
  if (null == body) body = '';

  // convert primitives
  body = body.valueOf();

  switch (typeof body) {
    // response status
    case 'number':
      if (!this.getHeader('Content-Type')) this.setHeader('Content-Type', 'text/plain');
      this.statusCode = body;
      body = http.STATUS_CODES[body];
      break;
    // string defaulting to html
    case 'string':
      if (!this.getHeader('Content-Type')) {
        this.charset = this.charset || 'utf-8';
        this.setHeader('Content-Type', 'text/html');
      }
      break;
    case 'boolean':
    case 'object':
      if (null == body) {
        body = '';
      } else if (Buffer.isBuffer(body)) {
        if (!this.getHeader('Content-Type')) this.setHeader('Content-Type', 'application/octet-stream');
      } else {
        return this.json(body);
      }
      break;
  }

  // populate Content-Length
  if (undefined !== body && !this.getHeader('Content-Length')) {
    this.setHeader('Content-Length', len = Buffer.isBuffer(body)
      ? body.length
      : Buffer.byteLength(body));
  }

  // ETag support
  // TODO: W/ support
  if (len > 1024) {
    var etag = String(Buffer.isBuffer(body)
      ? crc.buffer.crc32(body)
      : crc.crc32(body));
    if (!this.getHeader('ETag')) this.setHeader('ETag', etag);
  }

  // determine if it's cacheable
  var cache = 'GET' == method || 'HEAD' == method;
  if (cache) cache = (this.statusCode >= 200 && this.statusCode < 300) || 304 == this.statusCode;

  // freshness
  if (fresh(req.headers, this._headers) && cache) {
    this.statusCode = 304;
  }

  // strip irrelevant headers
  if (204 == this.statusCode || 304 == this.statusCode) {
    this.removeHeader('Content-Type');
    this.removeHeader('Content-Length');
    body = '';
  }

  // respond
  this.end('HEAD' == method ? null : body);
  return this;
};