示例#1
0
TorrentMetainfo::TorrentMetainfo(std::string filePath)
    : _pieces(new std::vector<Piece>), _totalLength(0)
{
    // Open the file
    fs::path p(filePath);
    std::ifstream input(p.string(), std::ios::in | std::ios::binary | std::ios::ate);
    if (input.fail())
    {
        THROW_EX(fileOpenFailedEx);
    }

    unsigned int size = static_cast<unsigned int>(input.tellg());

    // Read the file into an std::string
    std::string strData;
    strData.resize(size);
    input.seekg(0, std::ios::beg);
    input.read(&strData[0], size);

    bool succeeded;
    BencodeDictionary dict = parseBencoded<BencodeDictionary>(strData, &succeeded);
    if (!succeeded)
    {
        THROW_EX(bencodeParseFailedEx);
    }

    _parseAnnounceList(dict);

    _creationDate = dict.getInteger("creation date");
    _comment = dict.getString("comment");
    _author = dict.getString("created by");

    // Parse the "info" dictionary
    BencodeDictionary infoDict = dict.getDictionary("info");

    // Hash the info dictionary
    _infoDigest.resize(20);
    getSha1Hash(strData.c_str() + infoDict.beginIndex(),
                infoDict.endIndex() - infoDict.beginIndex(),
                &_infoDigest[0]);

    _pieceLength = static_cast<unsigned int>(infoDict.getInteger("piece length"));

    // Load each piece's hash into the list
    std::string piecesStr = infoDict.getString("pieces");
    _numPieces = piecesStr.size() / PIECE_HASH_SIZE;

    _pieces->resize(_numPieces);
    piecesStr.copy(reinterpret_cast<char*>(&_pieces->at(0)), piecesStr.size());

    _private = infoDict.keyExists("private")
        ? (infoDict.getInteger("private") == 1 ? true : false)
        : false;

    _parseFileList(infoDict);

    for (auto it = begin(_fileList); it != end(_fileList); it++)
    {
        _totalLength += it->length;
    }
}
示例#2
0
文件: Sign.c 项目: elnikety/crypsys
SigNat sign(PublicKey group, RogueKey rogue, Secret secret, Cert cert, Message msg)
{
  char bsn[MAX_CHAR_ARRAY_LENGTH], m[MAX_CHAR_ARRAY_LENGTH];
  char error[MAX_CHAR_ARRAY_LENGTH],c_h[MAX_CHAR_ARRAY_LENGTH];
  char hash[MAX_CHAR_ARRAY_LENGTH], inner[MAX_CHAR_ARRAY_LENGTH];
  BigInt n_v, c, sv, sf0, sf1, se, see, sew, sr, ser, sw;
  Origin b;
  BigInt n,gPrime,g,h,r0,r1,s;
  BigInt biga,e, p2;
  BigInt f0,f1,v;
  Tuple wr,rf0rf1,rwrr,rewrer;
  BigInt bigt1, bigt2, temp1, temp2, temp3, temp4, temp5, temp6, temp7, w, r;
  BigInt zeta,bign_V,rf0,rf1, rv, bigt2inv, tilde_TPrime2,n_t;
  BigInt tilde_T1t, tilde_rf, tilde_N_V,rw,rr,re,ree,rew, rer,hinv, tilde_T1, tilde_T2;
  Hash hashPub, hashHost ,hashTpm1, hashTpm2;
  Responses ss;
  SigNat signat;

  strcpy(bsn,msg.bsn); strcpy(m,msg.m);
  b = msg.b; n_v = msg.n_v;
  if(noOfBits(n_v) > hash_bits){
    strcpy(error,"n_v out of bounds");
    Abort(error);
  }
  n=group.n; gPrime=group.gPrime; g=group.g; h=group.h;
  r0=group.r0; r1=group.r1; s=group.s;
  
  biga = cert.biga; e = cert.e;
  f0 = secret.f; f1=secret.s; v=secret.t;
  wr = pick2(rsa_modulus_bits+distribution_bits);
  w = wr.r; r = wr.m;
  bigt1 = BImodMult(biga,BImodPower(h,wr.r,n),n);
  
  temp1 = BIpower(g,w); temp2 = BIpower(h,e); temp3 = BIpower(gPrime,r);
  temp4 = BImul(temp1, temp2); temp5 = BImul(temp4, temp3);
  bigt2 = BImod(temp5,n);
  
  zeta = base(bsn,rogue);
  bign_V = tag(rogue,zeta,f0,f1);

  rv = pick(random_bits+distribution_bits+hash_bits);
  rf0rf1 = pick2(halfkeyBits+distribution_bits+hash_bits);
  rf0 = rf0rf1.r; rf1 = rf0rf1.m;
  
  temp1 = BIpower(r0,rf0); temp2 = BIpower(r1,rf1); temp3 = BIpower(s,rv);
  temp4 = BImul(temp1, temp2); temp5 = BImul(temp4, temp3);
  tilde_T1t = BImod(temp5,n);

  temp1 = BIshiftLeft(rf1,halfkeyBits);
  temp2 = BIadd(rf0, temp1);
  tilde_rf = BImod(temp2,rogue.rho);
  tilde_N_V = BImodPower(zeta,tilde_rf,rogue.bigGamma);
  
  re = pick(prime_random_bits+distribution_bits+hash_bits);
  ree = pick(2*prime_total_bits+distribution_bits+hash_bits+1);
  rwrr = pick2(rsa_modulus_bits+2*distribution_bits+hash_bits);
  rw = rwrr.r; rr = rwrr.m;
  rewrer = pick2(prime_total_bits+rsa_modulus_bits+2*distribution_bits+hash_bits+1);
  rew = rewrer.r; rer = rewrer.m;
  hinv = BImodInv(h,n);

  temp1 = tilde_T1t; temp2 = BIpower(bigt1,re); temp3 = BIpower(hinv,rew);
  temp4 = BImul(temp1, temp2); temp5 = BImul(temp4, temp3);
  tilde_T1 = BImod(temp5,n);

  temp1 = BIpower(g,rw); temp2 = BIpower(h,re); temp3 = BIpower(gPrime,rr);
  temp4 = BImul(temp1, temp2); temp5 = BImul(temp4, temp3);
  tilde_T2 = BImod(temp5,n);
  
  bigt2inv = BImodInv(bigt2,n);
  temp1 = BIpower(bigt2inv,re); temp2 = BIpower(g,rew); temp3 = BIpower(h,ree); temp4 = BIpower(gPrime,rer);
  temp5 = BImul(temp1, temp2); temp6 = BImul(temp5, temp3); temp7 = BImul(temp6, temp4);
  tilde_TPrime2 = BImod(temp7,n);

  //hash public inputs
  hashPub = getSha1Hash();
  addBI(&hashPub,group.n); addBI(&hashPub,group.g); addBI(&hashPub,group.gPrime); addBI(&hashPub,group.h);
  addBI(&hashPub,group.r0); addBI(&hashPub,group.r1); addBI(&hashPub,group.s); addBI(&hashPub,group.z);
  addBI(&hashPub,rogue.gamma); addBI(&hashPub,rogue.bigGamma); addBI(&hashPub,rogue.rho);
  addBI(&hashPub,zeta); addBI(&hashPub,bigt1); addBI(&hashPub,bigt2); addBI(&hashPub,bign_V);
  strcpy(hash,hashResult(hashPub));

  //hash host inputs
  hashHost = getSha1Hash();
  addBI(&hashHost,tilde_T1); addBI(&hashHost,tilde_T2); addBI(&hashHost,tilde_TPrime2);
  addBI(&hashHost,tilde_N_V); addBI(&hashHost,n_v);
  strcpy(c_h,hashResult(hashHost));

  n_t = pick(distribution_bits);
  
  hashTpm1 = getSha1Hash();
  addBytes(&hashTpm1,c_h); 
  addBI(&hashTpm1,n_t); 
  strcpy(inner,hashResult(hashTpm1));
  hashTpm2 = getSha1Hash();
  addBytes(&hashTpm2,inner); 
  if(b == Card)
    addByte(&hashTpm2,0);
  else if (b == Verifier)
    addByte(&hashTpm2,1);
  else
    ;
  addBytes(&hashTpm2, m);
  c = BItoNumber(hashResult(hashTpm2));
  sv = rval(rv,v,c);
  sf0 = rval(rf0,f0,c);
  sf1 = rval(rf1,f1,c);
  p2 = BIshiftLeft(BItoNumber("1"),prime_total_bits-1);
  se = rval(re,BIsub(e, p2),c);
  see = rval(ree,BImul(e,e),c);
  sw = rval(rw,w,c);
  sew = rval(rew,BImul(e,w),c);
  sr = rval(rr,r,c);
  ser = rval(rer,BImul(e,r),c);

  ss.sv = sv; 
  ss.sf0 = sf0; 
  ss.sf1 = sf1; 
  ss.se = se; 
  ss.see = see; 
  ss.sw = sw; 
  ss.sew = sew; 
  ss.sr = sr; 
  ss.ser = ser;
  
  signat.zeta = zeta;
  signat.bigt1 = bigt1;
  signat.bigt2 = bigt2;
  signat.bign_V = bign_V;
  signat.c = c;
  signat.n_t = n_t;
  signat.ss = ss;

  return signat;
}