bool CryptoECDSA::VerifyData(SecureBinaryData const & binMessage, 
                             SecureBinaryData const & binSignature,
                             BTC_PUBKEY const & cppPubKey)
                            
{


   CryptoPP::SHA256  sha256;
   BTC_PRNG prng;

   assert(cppPubKey.Validate(prng, 3));

   // We execute the first SHA256 op, here.  Next one is done by Verifier
   SecureBinaryData hashVal(32);
   sha256.CalculateDigest(hashVal.getPtr(), 
                          binMessage.getPtr(), 
                          binMessage.getSize());

   // Verifying message 
   BTC_VERIFIER verifier(cppPubKey); 
   return verifier.VerifyMessage((const byte*)hashVal.getPtr(), 
                                              hashVal.getSize(),
                                 (const byte*)binSignature.getPtr(), 
                                              binSignature.getSize());
}
Esempio n. 2
0
extern "C" char *_dll_name(char *name)
{
    int hash = hashVal(name);
    HASHREC *hr  = _using_hash[hash];
    while (hr)
    {
        if (!strcmp(hr->p->name, name))
            return ((struct data *)hr->p)->dllName;
        hr = hr->next;
    }
    return NULL;
}
void analyzeFragment(string fragment) {
	string first_half = fragment.substr(0, KEY_LENGTH);
	string second_half;
	string index_string;
	int index;

	//If the first segment have has bad bp
	if (first_half.find('N') != string::npos)
		return;

	//Iterate all consecutive segments
	for (int i = 1; i <= MAX_SEGMENT_NUM_; i++) {
		//Iterate In/Dels
		for (int j = 0; j <= MAX_INDEL_NUM_; j++) {
			//For insertions
			second_half = fragment.substr(i * KEY_LENGTH - j, KEY_LENGTH);
			if (second_half.find('N') == string::npos) {

				index_string = first_half.substr(0, KEY_LENGTH / 2)
						+ second_half.substr(0, KEY_LENGTH / 2);
				index = hashVal(index_string);
				for (int k = j; k <= MAX_INDEL_NUM_; k++) {
					friend_hash[index][i][k] = true;
				}
			}

			//For deletions
			second_half = fragment.substr(i * KEY_LENGTH + j, KEY_LENGTH);
			if (second_half.find('N') == string::npos) {

				index_string = first_half.substr(0, KEY_LENGTH / 2)
						+ second_half.substr(0, KEY_LENGTH / 2);
				index = hashVal(index_string);
				for (int k = j; k <= MAX_INDEL_NUM_; k++) {
					friend_hash[index][i][k] = true;
				}
			}
		}
	}
}
Esempio n. 4
0
static void hashInsert(struct data *names)
{
    int hash = hashVal(names->name);
    HASHREC *hr  = _using_hash[hash];
    while (hr)
    {
        if (!strcmp(hr->p->name, names->name))
            return;
        hr = hr->next;
    }
    
    hr = (HASHREC *)calloc(1, sizeof(HASHREC));
    hr->p = (HASHREC::_hrintern_ *)names;
    hr->next = _using_hash[hash];
    _using_hash[hash] = hr;
}
Esempio n. 5
0
/**************************************************************************
	//Function:			DelNode
	//Description:		删除节点
	//Calls:
	//Called by:
	//Input:
						pNode				指向待删除节点的指针
						lFlag				1表示删除且需要释放内存 0表示删除但不需要释放内存
	//Output:
	//Return:
						返回值				说明
						0					成功
						负值				失败
	//Others:
	//Author:	fanyh	Email: [email protected]
	//Date:		2008-06-16
	**************************************************************************/
int
DelNode(CMiniCache *cache, CMiniCacheNode *pNode, long lFlag)
{
  if(!pNode) return -1;

  if(pNode->pLeft && pNode->pRight) {
    pNode->pLeft->pRight = pNode->pRight;
    pNode->pRight->pLeft = pNode->pLeft;
  } else {
    if(NULL == pNode->pRight) {
      //m_pHead
      cache->m_pHead = pNode->pLeft;
      if(cache->m_pHead)
        cache->m_pHead->pRight = NULL;
    }
    if(NULL == pNode->pLeft) {
      //m_pEnd
      cache->m_pEnd = pNode->pRight;
      if(cache->m_pEnd)
        cache->m_pEnd->pLeft = NULL;
    }
  }

  size_t pos = hashVal(pNode->tKey, cache->m_lHashSize);
  if(pNode == cache->m_ppHash[pos]) {
    cache->m_ppHash[pos] = pNode->pDown;
    if(pNode->pDown)
      pNode->pDown->pUp = NULL;
  } else {
    if(pNode->pUp)
      pNode->pUp->pDown = pNode->pDown;
    if(pNode->pDown)
      pNode->pDown->pUp = pNode->pUp;
  }

  if(lFlag) {
    if(cache->m_pDataFreeFun && pNode->vpData) {
      while(cache->m_pDataFreeFun(pNode->vpData)) usleep(100);
      pNode->vpData = NULL;
      SYNC_SUB(&cache->lDataSize, pNode->lDataSize);
      SYNC_SUB(&cache->lDataNum, 1);
    }
    pNode->pDown = cache->m_pIdleNode;
    cache->m_pIdleNode = pNode;
  }
  return 0;
}
Esempio n. 6
0
/**************************************************************************
	//Function:			FindNode
	//Description:		根据关键字查找节点
	//Calls:
	//Called by:
	//Input:
						tKey				记录主关键字
	//Output:
						pNode				指向查找到节点的指针
	//Return:
						返回值				说明
						0					成功
						负值				失败
	//Others:
	//Author:	fanyh	Email: [email protected]
	//Date:		2008-06-16
	**************************************************************************/
int
FindNode(CMiniCache *cache, map_key_t tKey, CMiniCacheNode **pOldNode)
{
  size_t lPos;
  if (!pOldNode) return -1;

  lPos = hashVal(tKey, cache->m_lHashSize);

  if(cache->m_ppHash[lPos]) {
    *pOldNode = cache->m_ppHash[lPos];
    while(*pOldNode) {
      if(md5_compare((*pOldNode)->tKey->digest, tKey->digest) == 0)
        return 0;
      else
	*pOldNode = (*pOldNode)->pDown;
    }
  }
  return -1;
}
Esempio n. 7
0
void hashTestFull(char * hash_file_name, char * ref_file_name, char * output_file_name) {
	int  fragment_pointer;
	int  fragment_number;
	int  fragment_coord;
        int  * index_db;
        int  * coordinate_db;
	char * fragment_seq     = (char*) malloc(KEY_LENGTH+1);
	char * reconstructed_seq= (char *)malloc(sizeof(char)*KEY_LENGTH);
        char * decoded_char	= (char *)malloc(sizeof(char)*(KEY_LENGTH+1));
        FILE * pFileOut;


	fprintf (stdout,"Read Hash Table: %s \n", hash_file_name);
	hashReconstructorChar(&index_db, &coordinate_db, hash_file_name);
	pFileOut  = fopen (output_file_name, "w");
	for (int i = 0 ; i < INDEX_NUM ; i++) { 
		reconstructSeq(decoded_char, i);
		fragment_pointer = index_db[hashVal(decoded_char)]; 
		fragment_number  = coordinate_db[fragment_pointer];
		if (fragment_number != 0 ){
			fprintf (pFileOut,"\nseq %s: pointer %i: frag# %i---->", decoded_char, fragment_pointer, fragment_number);
		}
		for (int j = 0 ; j < fragment_number ; j++) {
			fragment_coord = coordinate_db[fragment_pointer+1+j];
			getRefSeq(fragment_seq, fragment_coord, KEY_LENGTH, REF_TABLE_SIZE);
			if (strncmp(fragment_seq, decoded_char, KEY_LENGTH) == 0) {	
				fprintf (pFileOut,"_P%i", j);
			} else {
				fprintf (pFileOut,"_F%i(%i/%s)", j, fragment_coord, fragment_seq);
			}
		}
	}
	fclose(pFileOut);
// free allocated memory
 	free(fragment_seq);
	free(reconstructed_seq);
        free(decoded_char);
}
Esempio n. 8
0
/**************************************************************************
  //Function:			__AddNode
  //Description:		加入关键字节点
  //Calls:
  //Called by:
  //Input:
            pNode				指向待加入节点的指针
  //Output:

  //Return:
            返回值				说明
            0					成功
            负值				失败
  //Others:
  //Author:	fanyh	Email: [email protected]
  //Date:		2008-06-16
  **************************************************************************/
static inline int
__AddNode(CMiniCache *cache, CMiniCacheNode* pNode, bool readd)
{
  size_t lPos;
  lPos = hashVal(pNode->tKey, cache->m_lHashSize);

  //放到hash位置
  if(cache->m_ppHash[lPos]) {
    pNode->pDown = cache->m_ppHash[lPos];
    pNode->pUp = NULL;
    cache->m_ppHash[lPos]->pUp = pNode;
    cache->m_ppHash[lPos] = pNode;
  } else {
    pNode->pDown = NULL;
    pNode->pUp = NULL;
    cache->m_ppHash[lPos] = pNode;
  }

  //使用链表,left是更加新的
  if(cache->m_pEnd) {
    pNode->pRight = cache->m_pEnd;
    pNode->pLeft = NULL;
    cache->m_pEnd->pLeft = pNode;
    cache->m_pEnd = pNode;
  } else {
    pNode->pRight = NULL;
    pNode->pLeft = NULL;
    cache->m_pEnd = pNode;
  }

  if(!cache->m_pHead)
    cache->m_pHead = pNode;
  if (!readd) {
    SYNC_ADD(&cache->lDataSize, pNode->lDataSize);
    SYNC_ADD(&cache->lDataNum, 1);
  }
  return 0;
}
/////////////////////////////////////////////////////////////////////////////
// Use the secp256k1 curve to sign data of an arbitrary length.
// Input:  Data to sign  (const SecureBinaryData&)
//         The private key used to sign the data  (const BTC_PRIVKEY&)
//         A flag indicating if deterministic signing is used  (const bool&)
// Output: None
// Return: The signature of the data  (SecureBinaryData)
SecureBinaryData CryptoECDSA::SignData(SecureBinaryData const & binToSign, 
                                       BTC_PRIVKEY const & cppPrivKey,
                                       const bool& detSign)
{

   // We trick the Crypto++ ECDSA module by passing it a single-hashed
   // message, it will do the second hash before it signs it.  This is 
   // exactly what we need.
   CryptoPP::SHA256  sha256;
   BTC_PRNG prng;

   // Execute the first sha256 op -- the signer will do the other one
   SecureBinaryData hashVal(32);
   sha256.CalculateDigest(hashVal.getPtr(), 
                          binToSign.getPtr(), 
                          binToSign.getSize());

   // Do we want to use a PRNG or use deterministic signing (RFC 6979)?
   string signature;
   if(detSign)
   {
      BTC_DETSIGNER signer(cppPrivKey);
      CryptoPP::StringSource(
         hashVal.toBinStr(), true, new CryptoPP::SignerFilter(
         prng, signer, new CryptoPP::StringSink(signature)));
   }
   else
   {
      BTC_SIGNER signer(cppPrivKey);
      CryptoPP::StringSource(
         hashVal.toBinStr(), true, new CryptoPP::SignerFilter(
         prng, signer, new CryptoPP::StringSink(signature)));
   }

   return SecureBinaryData(signature);
}
Esempio n. 10
0
SecureBinaryData CryptoECDSA::SignData(SecureBinaryData const & binToSign, 
                                       BTC_PRIVKEY const & cppPrivKey)
{

   // We trick the Crypto++ ECDSA module by passing it a single-hashed
   // message, it will do the second hash before it signs it.  This is 
   // exactly what we need.
   static CryptoPP::SHA256  sha256;
   static BTC_PRNG prng;

   // Execute the first sha256 op -- the signer will do the other one
   SecureBinaryData hashVal(32);
   sha256.CalculateDigest(hashVal.getPtr(), 
                          binToSign.getPtr(), 
                          binToSign.getSize());

   string signature;
   BTC_SIGNER signer(cppPrivKey);
   CryptoPP::StringSource(
               hashVal.toBinStr(), true, new CryptoPP::SignerFilter(
               prng, signer, new CryptoPP::StringSink(signature))); 
  
   return SecureBinaryData(signature);
}
Esempio n. 11
0
void *preProcessReads(int *idp)
{
	int id = *idp;
	int i=0, j=0, pos=0, tmpSize=0;

	int32_t	hvtmp, cstmp;

	int div = _r_seqCnt / THREAD_COUNT;
	div += (_r_seqCnt % THREAD_COUNT)?1:0;
	Pair *tmp = getMem(sizeof(Pair)*(div * _r_samplingLocsSize*2));
	char alphCnt[5];
	char *a, *b;

	for (i=id*div; i<div*(id+1) && i<_r_seqCnt; i++)
	{

		alphCnt[0]=alphCnt[1]=alphCnt[2]=alphCnt[3]=alphCnt[4]=0;
		a = _r_seq[i].seq;
		b = _r_seq[i].rseq+SEQ_LENGTH;
		*(b--)='\0';
		for (j = 0; j<SEQ_LENGTH; j++)
		{
			*a = toupper(*a);
			*b = reverseCompleteChar(*a);
			alphCnt[_r_alphIndex[*a]]++;
			a++;
			b--;			
		}

		if (alphCnt[4]>errThreshold)
			_r_seq[i].hits[0]=1;

		_r_seq[i].alphCnt[0] = alphCnt[0];
		_r_seq[i].alphCnt[1] = alphCnt[1];
		_r_seq[i].alphCnt[2] = alphCnt[2];
		_r_seq[i].alphCnt[3] = alphCnt[3];

		compressSequence(_r_seq[i].seq, SEQ_LENGTH, _r_seq[i].cseq);
		compressSequence(_r_seq[i].rseq, SEQ_LENGTH, _r_seq[i].crseq);

		if (_r_seq[i].hits[0] == 1)			// marked reads are not indexed
		{
			_r_seq[i].hits[0] = 0;
			for (j=0; j< 2*_r_samplingLocsSize; j++)
			{
				tmp[pos].hv = -1;
				tmp[pos].checksum = 0;
				tmp[pos].seqInfo = pos +(div*id*2*_r_samplingLocsSize);
				pos++;
			}
		}
		else
		{
			for (j=0; j< _r_samplingLocsSize; j++)
			{
				hvtmp = hashVal(_r_seq[i].seq+_r_samplingLocs[j]);
				cstmp = checkSumVal(_r_seq[i].seq+_r_samplingLocs[j]+WINDOW_SIZE);
				if (hvtmp == -1 || cstmp == -1)
				{
					tmp[pos].hv = -1;
					tmp[pos].checksum = 0;
				}
				else
				{
					tmp[pos].hv = hvtmp;
					tmp[pos].checksum = cstmp;
				}
				tmp[pos].seqInfo = pos +(div*id*2*_r_samplingLocsSize);
				pos++;
			}

			for (j=0; j<_r_samplingLocsSize; j++)
			{
				hvtmp = hashVal(_r_seq[i].rseq+_r_samplingLocs[j]);
				cstmp = checkSumVal(_r_seq[i].rseq+_r_samplingLocs[j]+WINDOW_SIZE);
				
				if (hvtmp == -1  || cstmp == -1)
				{
					tmp[pos].hv = -1;
					tmp[pos].checksum = 0;
				}
				else
				{
					tmp[pos].hv = hvtmp;
					tmp[pos].checksum = cstmp;
				}
				tmp[pos].seqInfo = pos+(div*id*2*_r_samplingLocsSize);
				pos++;
			}

		}
		tmpSize+=2*_r_samplingLocsSize;
	}
	
	introSortPair( tmp, 0, tmpSize-1);

	int uniq = 0;
	int prev = -2;
	int beg = -1;
	int end = -1;

	for (i=0; i<tmpSize; i++)
	{
		if (prev != tmp[i].hv)
		{
			uniq ++;
			prev = tmp[i].hv;
		}
	}

	_r_readIndexSize[id] = uniq;
	_r_readIndex[id] = getMem(sizeof(ReadIndexTable)*_r_readIndexSize[id]);


	prev = -2;
	j=0;
	beg =0;
	while (beg < tmpSize)
	{
		end = beg;
		while (end+1<tmpSize && tmp[end+1].hv==tmp[beg].hv)
			end++;

		_r_readIndex[id][j].hv = tmp[beg].hv;

		_r_readIndex[id][j].list = getMem(sizeof(GeneralIndex)*(end-beg+2));
		_r_readIndex[id][j].list[0].info = end-beg+1;

		for (i=1; i <= _r_readIndex[id][j].list[0].info; i++)
		{
			_r_readIndex[id][j].list[i].info=tmp[beg+i-1].seqInfo;
			_r_readIndex[id][j].list[i].checksum=tmp[beg+i-1].checksum;
		}

		j++;
		beg = end+1;
	}
	freeMem(tmp, sizeof(Pair)*(div*_r_samplingLocsSize*2));
	return NULL;
}
void hashTableGenerator(string ref_name) {
        //Open and read the reference file
        ifstream ref_file;
        deque<int>::iterator iter;
        int coor_counter = KEY_LENGTH*(-1);
        int flag_ignore = 0;
        int monitor_counter  = 0;       // For operation monitoring
        int monitor_counter2 = 0;       // For operation monitoring

        ref_file.open(ref_name.c_str());
        if (!ref_file.good()) {
                cerr << "reference file cannot open." << endl;
                exit(1);
        }

        //cout << "Hello0" << endl;

        //Initialize hash_table.
        //      hash_table.resize(1 << (2 * KEY_LENGTH) );
        //      hash_table_counter = 0;

        //cout << "Hello1" << endl;

        //Read in the first key
        char temp_key[KEY_LENGTH + 1];
        char temp_char;
        string keystr(KEY_LENGTH, 'A');
        //cout << "keystr: " << keystr << endl;

        //throw out the first several Ns
        do {
                ref_file >> temp_char;
                coor_counter++;
                //cout << "coor_counter: " << coor_counter << endl;
        } while (temp_char == 'N');

        //update the first non N character
        temp_key[0] = temp_char;
        //cout << "temp_char: " << temp_char << endl;

        //Read the first 12 characters
        for (int i = 1; i < KEY_LENGTH; i++) {
                //cout << "i: " << i << endl;
                ref_file >> temp_char;
                coor_counter++;
                //cout << "coor_counter: " << coor_counter << endl;
                temp_key[i] = temp_char;

                //Check if the file is too short
                if (ref_file.eof()) {
                        cerr << "file too short";
                        exit(1);
                }
                //Check if next N come too close
                if (temp_char == 'N') {
                        cerr << "Too short useful String A" << endl;
                        exit(1);
                }
        }

        temp_key[KEY_LENGTH] = '\0';
        keystr = temp_key;
        //cout << "temp_key: " << temp_key << endl;
        //cout << "keystr: " << keystr << endl;

        //Read the ref file until the end of file.
        do {
                //Compute idx and hash_value
                if (flag_ignore == 0) {
                        int hash_value = hashVal(keystr);
                //cout << "hash_value: " << hash_value << endl;

                //Add to hash_table
                //cout << "coor_counter: " << coor_counter << endl;
                        hash_table[hash_value].push_front(coor_counter);
                        hash_table_counter++;
                } else {
                        flag_ignore = 0;
                        for (int i = 0; i < KEY_LENGTH; i++ ) {
                                if (keystr[i] == 'N') {
                                        flag_ignore = 1;
                                }       
                        }
			if (flag_ignore == 0) {
                        	int hash_value = hashVal(keystr);
                        	//hash_table[hash_value].push_front(coor_counter);
                        	//hash_table_counter++;
			}
                }
                /*
                 //Add to hash_table list
                 if (hash_table[idx][hash_value].size() == 0) {
                 //cout << "first saw" << endl;
                 hash_table[idx][hash_value].push_back(counter);
                 }
                 else {
                 iter = hash_table[idx][hash_value].begin();
                 while (iter != hash_table[idx][hash_value].end() && counter > *iter) {
                 //cout << "*iter: " << *iter << endl;
                 iter++;
                 }
                 hash_table[idx][hash_value].insert(iter, counter);
                 }
                 */

                //Read next character
                ref_file >> temp_char;
                //Increment counter
                coor_counter++;

                bool fresh_start = false; //indicater for a new start after large area of N
                //If enters N area, throw them away
                while (temp_char == 'N') {
                        if (ref_file.eof() ) {
                                ref_file.close();
                                return;
                        }
                        fresh_start = true;
                        ref_file >> temp_char;
                        coor_counter++;
                        //cout << "coor_counter: " << coor_counter << endl;
                }
                if (fresh_start) {
                        //cout << "In a fresh start!" << endl;
                        keystr[0] = temp_char;
                        for (int i = 1; i < KEY_LENGTH; i++) {
                                //cout << 'i' << i << endl;
                                ref_file >> temp_char;
                                coor_counter++;
                                //cout << "coor_counter: " << coor_counter << endl;
                                //Check if next N come too close
                                if (temp_char == 'N') {
                                        cerr << "Too short useful String B" << endl;
                                        flag_ignore = 1;
                                        //exit(1);
                                }
                                keystr[i] = temp_char;
                        }
                } else
                        keystr = keystr.substr(1, KEY_LENGTH - 1) + temp_char;
                //cout << "keystr: " << keystr << endl;
                // add process monitoring loop
                monitor_counter  = monitor_counter  +1;         // For operation monitoring
                monitor_counter2 = monitor_counter2 +1;         
                if (monitor_counter >= 1000000) {
                        cout << "hash generator: " << monitor_counter2 << endl;
                        monitor_counter = 0;
                }
        } while (ref_file.good());