| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3446 人关注过本帖
标题:bt种子文件生成
只看楼主 加入收藏
郭胖
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2011-5-5
结帖率:83.33%
收藏
 问题点数:0 回复次数:6 
bt种子文件生成
小弟自己写了一个bt种子文件生成的程序,只是用C语言模仿生成过程而已,我还没写完,急着运行一下,结果编译通过了但是运行不了,高手帮我看看吧~~
/*==============================================sha1散列值计算相关函数========================
 *  sha1.c
 *
 *  Copyright (C) 1998, 2009
 *  Paul E. Jones <paulej@
 *  All Rights Reserved
 *
 *****************************************************************************
 *  $Id: sha1.c 12 2009-06-22 19:34:25Z paulej $
 *****************************************************************************
 *
 *  Description:
 *      This file implements the Secure Hashing Standard as defined
 *      in FIPS PUB 180-1 published April 17, 1995.
 *
 *      The Secure Hashing Standard, which uses the Secure Hashing
 *      Algorithm (SHA), produces a 160-bit message digest for a
 *      given data stream.  In theory, it is highly improbable that
 *      two messages will produce the same message digest.  Therefore,
 *      this algorithm can serve as a means of providing a "fingerprint"
 *      for a message.
 *
 *  Portability Issues:
 *      SHA-1 is defined in terms of 32-bit "words".  This code was
 *      written with the expectation that the processor has at least
 *      a 32-bit machine word size.  If the machine word size is larger,
 *      the code should still function properly.  One caveat to that
 *      is that the input functions taking characters and character
 *      arrays assume that only 8 bits of information are stored in each
 *      character.
 *
 *  Caveats:
 *      SHA-1 is designed to work with messages less than 2^64 bits
 *      long. Although SHA-1 allows a message digest to be generated for
 *      messages of any number of bits less than 2^64, this
 *      implementation only works with messages with a length that is a
 *      multiple of the size of an 8-bit character.
 *
 */

# include "sha1.h"
# include <stdio.h>
# include <string.h>
# include <conio.h>


/*
 *  Define the circular shift macro
 */
#define SHA1CircularShift(bits,word) \
                ((((word) << (bits)) & 0xFFFFFFFF) | \
                ((word) >> (32-(bits))))

/* Function prototypes */
void SHA1ProcessMessageBlock(SHA1Context *);
void SHA1PadMessage(SHA1Context *);

/*  
 *  SHA1Reset
 *
 *  Description:
 *      This function will initialize the SHA1Context in preparation
 *      for computing a new message digest.
 *
 *  Parameters:
 *      context: [in/out]
 *          The context to reset.
 *
 *  Returns:
 *      Nothing.
 *
 *  Comments:
 *
 */
void SHA1Reset(SHA1Context *context)
{
    context->Length_Low             = 0;
    context->Length_High            = 0;
    context->Message_Block_Index    = 0;

    context->Message_Digest[0]      = 0x67452301;
    context->Message_Digest[1]      = 0xEFCDAB89;
    context->Message_Digest[2]      = 0x98BADCFE;
    context->Message_Digest[3]      = 0x10325476;
    context->Message_Digest[4]      = 0xC3D2E1F0;

    context->Computed   = 0;
    context->Corrupted  = 0;
}

/*  
 *  SHA1Result
 *
 *  Description:
 *      This function will return the 160-bit message digest into the
 *      Message_Digest array within the SHA1Context provided
 *
 *  Parameters:
 *      context: [in/out]
 *          The context to use to calculate the SHA-1 hash.
 *
 *  Returns:
 *      1 if successful, 0 if it failed.
 *
 *  Comments:
 *
 */
int SHA1Result(SHA1Context *context)
{

    if (context->Corrupted)
    {
        return 0;
    }

    if (!context->Computed)
    {
        SHA1PadMessage(context);
        context->Computed = 1;
    }

    return 1;
}

/*  
 *  SHA1Input
 *
 *  Description:
 *      This function accepts an array of octets as the next portion of
 *      the message.
 *
 *  Parameters:
 *      context: [in/out]
 *          The SHA-1 context to update
 *      message_array: [in]
 *          An array of characters representing the next portion of the
 *          message.
 *      length: [in]
 *          The length of the message in message_array
 *
 *  Returns:
 *      Nothing.
 *
 *  Comments:
 *
 */
void SHA1Input(     SHA1Context         *context,
                    const unsigned char *message_array,
                    unsigned   long int         length)
{
    if (!length)
    {
        return;
    }

    if (context->Computed || context->Corrupted)
    {
        context->Corrupted = 1;
        return;
    }

    while(length-- && !context->Corrupted)
    {
        context->Message_Block[context->Message_Block_Index++] =
                                                (*message_array & 0xFF);

        context->Length_Low += 8;
        /* Force it to 32 bits */
        context->Length_Low &= 0xFFFFFFFF;
        if (context->Length_Low == 0)
        {
            context->Length_High++;
            /* Force it to 32 bits */
            context->Length_High &= 0xFFFFFFFF;
            if (context->Length_High == 0)
            {
                /* Message is too long */
                context->Corrupted = 1;
            }
        }

        if (context->Message_Block_Index == 64)
        {
            SHA1ProcessMessageBlock(context);
        }

        message_array++;
    }
}

/*  
 *  SHA1ProcessMessageBlock
 *
 *  Description:
 *      This function will process the next 512 bits of the message
 *      stored in the Message_Block array.
 *
 *  Parameters:
 *      None.
 *
 *  Returns:
 *      Nothing.
 *
 *  Comments:
 *      Many of the variable names in the SHAContext, especially the
 *      single character names, were used because those were the names
 *      used in the publication.
 *         
 *
 */
void SHA1ProcessMessageBlock(SHA1Context *context)
{
    const unsigned K[] =            /* Constants defined in SHA-1   */      
    {
        0x5A827999,
        0x6ED9EBA1,
        0x8F1BBCDC,
        0xCA62C1D6
    };
    int         t;                  /* Loop counter                 */
    unsigned    temp;               /* Temporary word value         */
    unsigned    W[80];              /* Word sequence                */
    unsigned    A, B, C, D, E;      /* Word buffers                 */

    /*
     *  Initialize the first 16 words in the array W
     */
    for(t = 0; t < 16; t++)
    {
        W[t] = ((unsigned) context->Message_Block[t * 4]) << 24;
        W[t] |= ((unsigned) context->Message_Block[t * 4 + 1]) << 16;
        W[t] |= ((unsigned) context->Message_Block[t * 4 + 2]) << 8;
        W[t] |= ((unsigned) context->Message_Block[t * 4 + 3]);
    }

    for(t = 16; t < 80; t++)
    {
       W[t] = SHA1CircularShift(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]);
    }

    A = context->Message_Digest[0];
    B = context->Message_Digest[1];
    C = context->Message_Digest[2];
    D = context->Message_Digest[3];
    E = context->Message_Digest[4];

    for(t = 0; t < 20; t++)
    {
        temp =  SHA1CircularShift(5,A) +
                ((B & C) | ((~B) & D)) + E + W[t] + K[0];
        temp &= 0xFFFFFFFF;
        E = D;
        D = C;
        C = SHA1CircularShift(30,B);
        B = A;
        A = temp;
    }

    for(t = 20; t < 40; t++)
    {
        temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[1];
        temp &= 0xFFFFFFFF;
        E = D;
        D = C;
        C = SHA1CircularShift(30,B);
        B = A;
        A = temp;
    }

    for(t = 40; t < 60; t++)
    {
        temp = SHA1CircularShift(5,A) +
               ((B & C) | (B & D) | (C & D)) + E + W[t] + K[2];
        temp &= 0xFFFFFFFF;
        E = D;
        D = C;
        C = SHA1CircularShift(30,B);
        B = A;
        A = temp;
    }

    for(t = 60; t < 80; t++)
    {
        temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[3];
        temp &= 0xFFFFFFFF;
        E = D;
        D = C;
        C = SHA1CircularShift(30,B);
        B = A;
        A = temp;
    }

    context->Message_Digest[0] =
                        (context->Message_Digest[0] + A) & 0xFFFFFFFF;
    context->Message_Digest[1] =
                        (context->Message_Digest[1] + B) & 0xFFFFFFFF;
    context->Message_Digest[2] =
                        (context->Message_Digest[2] + C) & 0xFFFFFFFF;
    context->Message_Digest[3] =
                        (context->Message_Digest[3] + D) & 0xFFFFFFFF;
    context->Message_Digest[4] =
                        (context->Message_Digest[4] + E) & 0xFFFFFFFF;

    context->Message_Block_Index = 0;
}

/*  
 *  SHA1PadMessage
 *
 *  Description:
 *      According to the standard, the message must be padded to an even
 *      512 bits.  The first padding bit must be a '1'.  The last 64
 *      bits represent the length of the original message.  All bits in
 *      between should be 0.  This function will pad the message
 *      according to those rules by filling the Message_Block array
 *      accordingly.  It will also call SHA1ProcessMessageBlock()
 *      appropriately.  When it returns, it can be assumed that the
 *      message digest has been computed.
 *
 *  Parameters:
 *      context: [in/out]
 *          The context to pad
 *
 *  Returns:
 *      Nothing.
 *
 *  Comments:
 *
 */
void SHA1PadMessage(SHA1Context *context)
{
    /*
     *  Check to see if the current message block is too small to hold
     *  the initial padding bits and length.  If so, we will pad the
     *  block, process it, and then continue padding into a second
     *  block.
     */
    if (context->Message_Block_Index > 55)
    {
        context->Message_Block[context->Message_Block_Index++] = 0x80;
        while(context->Message_Block_Index < 64)
        {
            context->Message_Block[context->Message_Block_Index++] = 0;
        }

        SHA1ProcessMessageBlock(context);

        while(context->Message_Block_Index < 56)
        {
            context->Message_Block[context->Message_Block_Index++] = 0;
        }
    }
    else
    {
        context->Message_Block[context->Message_Block_Index++] = 0x80;
        while(context->Message_Block_Index < 56)
        {
            context->Message_Block[context->Message_Block_Index++] = 0;
        }
    }

    /*
     *  Store the message length as the last 8 octets
     */
    context->Message_Block[56] = (context->Length_High >> 24) & 0xFF;
    context->Message_Block[57] = (context->Length_High >> 16) & 0xFF;
    context->Message_Block[58] = (context->Length_High >> 8) & 0xFF;
    context->Message_Block[59] = (context->Length_High) & 0xFF;
    context->Message_Block[60] = (context->Length_Low >> 24) & 0xFF;
    context->Message_Block[61] = (context->Length_Low >> 16) & 0xFF;
    context->Message_Block[62] = (context->Length_Low >> 8) & 0xFF;
    context->Message_Block[63] = (context->Length_Low) & 0xFF;

    SHA1ProcessMessageBlock(context);
}//======================================sha1散列值计算===================================













typedef struct                //单文件模式下的种子
{
 struct                       //单文件模式下的Info键
    {
      unsigned long int piece_length;       //该键对应的值是每一片(piece)的字节数
      char pieces[2048][128];     /*该键对应的值由所有的20字节SHA1散列值连接而成的字符串
                               每一片(piece)均含有一个唯一的SHA1散列值。最大可制作1G文件的种子*/
       //int _private;      
      char name[40];          //文件名
      unsigned long int length;             //文件的所占字节数
     }info;     
  char announce[100];
  int creation_date[6];
  char comment[150],
       create_by[50];
  char encoding[100];

}seed;
typedef struct                  //多文件模式下的种子文件
{
  struct                  //多文件模式下的Info键
    {
         unsigned long int piece_length;
         char pieces[2048][128];
         //int _private;
         char name[40];
         unsigned long int length[10];
         char path[100];             //包含单个或多个元素的list,这些元素合成在一起表示文件路径和文件名
      }info;            
  char announce[100];            //tracker的announce URL
  int creation_date[6];          //torrent文件的创建时间  格式 0000-00-00 00:00:00
  char comment[150],             //torrent文件制作者的评论
       create_by[50];            //制作torrent文件的程序的名称和版本
  char encoding[100];             /*torrent元文件中包含一个info dictionary,当该dictionary过大时,
                                   就需要对其分片(piece),该编码就是用来生成分片的*/
}seeds;


unsigned long int Get_file_size(FILE * fp)//------------获取文件大小函数
{     
      int lastPos;
      int currentPos = ftell(fp);        //取得当前文件指针位置,可能已经移动了文件指针
      fseek(fp, 0, SEEK_END);            //移动到文件的结尾  
      lastPos = ftell(fp);               //获得文件大小
      fseek(fp,currentPos,SEEK_SET);     //恢复到原来的文件指针位置
      return lastPos;
}


void sha1(char temp[],seed * torrent,int a)//-------------------------sha1散列值计算函数
{
    FILE * fp_temp;
    SHA1Context sha;
    int i;
    SHA1Reset(&sha);
    SHA1Input(&sha, (const unsigned char *) temp,torrent->info.piece_length);
    if (!SHA1Result(&sha))
        fprintf(stderr, "ERROR-- could not compute message digest\n");
    else
    {   fp_temp=fopen("abc.txt","w");
        for(i = 0; i < 5 ; i++)
             fprintf(fp_temp,"%d",sha.Message_Digest[i]);
        fclose(fp_temp);
    }
    fp_temp=fopen("abc.txt","r");
    fgets(torrent->info.pieces[a],128,fp_temp);
    fclose(fp_temp);
}







void make_torrent(FILE * fp,char temp[],seed * torrent)//------------------------------------单个文件的种子制作函数
{
    int i;
    unsigned long int number;
    rewind(fp);
   
    number=(torrent->info.length)/(torrent->info.piece_length)+1;//--------获取分片个数
    for ( i=0;i<=number;i++)//-------------------------------------------得到每个分片的sha1散列值
    {
       fseek(fp,i*(torrent->info.piece_length),0);
       fread(temp,torrent->info.piece_length,1,fp);
       sha1(temp,torrent,i);
     }

}


int main()
{
   char temp[1024*1024];
   FILE * fp;
   seed * torrent;
   char * file_name="name.txt";
   char a;//-------------------------------------------------------用于程序停顿的字符变量
   int choice;
   printf ("=====================================\n");//--------------欢迎界面
   printf ("==WELCOME TO THE Bittorrent PROGRAM==\n");
   printf ("=====================================\n");
   printf ("===========created by\n");
   printf ("正在开始未完成的下载…………\n");
   a=getchar();//--------------------------------------------------进行下一步前的停顿
menu : printf ("==========请选择功能===========\n");//--主选单
   printf ("1:制作torrent\n");
   printf ("2:添加torrent(默认位置添加)\n");
   printf ("3:查看正在下载的数据\n");
   printf ("4:查看正在上传的数据\n");
   printf ("5:退出\n");
   scanf ("%d",&choice);
   switch (choice)
   {
     case 1 :goto make_seed;
     //case 2 :printf("2");goto f2;
     //case 3 :printf("3");goto f3;
     //case 4 :printf("4");goto f4;
     case 5 :return 0;
     default : printf("选择错误,按enter键回到主选单\n");a=getchar();a=getchar();goto menu;
   }
make_seed : printf ("============torrent制作=============\n");//------torrent制作界面
    printf ("1:单个文件的torrent制作\n");
    printf ("2:多个文件的torrent制作\n");
    printf ("3:返回主选单\n");
    scanf ("%d",&choice);
    switch (choice)
    {
      case 1 :goto sigle;
     // case 2 :goto mult;
      case 3 :goto menu;
      default :printf("选择错误,按enter键返回torrent制作菜单");a=getchar();a=getchar();goto make_seed;
    }
 
sigle :printf ("\n请输入文件名(包含后缀)\n");
       scanf ("%s",file_name);
       if ((fp=fopen (file_name,"r"))==NULL)
          {
            printf ("文件打开失败\n");
            goto make_seed;
           }
       torrent->info.length=Get_file_size(fp);//--------------获取文件大小
       if (torrent->info.length<=(100*1024*1024))//----------确定分块大小
           torrent->info.piece_length=256*1024;
       else if (torrent->info.length>=(1024*1024*1024))
              torrent->info.piece_length=1024*1024;
            else torrent->info.piece_length=512*1024;
        
       make_torrent (fp,temp,torrent);//------------------生成种子文件
       if (a!=0)
           printf ("种子制作失败,按enter键返回种子制作菜单\n");
       else printf ("种子文件制作成功,保存在默认文件夹下\n");
            printf ("按enter键返回主选单\n");
            goto menu;
      // mult :a=getchar();
   return 0;
}

[ 本帖最后由 郭胖 于 2011-6-23 16:26 编辑 ]
搜索更多相关主题的帖子: Copyright bt种子 C语言 
2011-06-23 16:24
voidx
Rank: 12Rank: 12Rank: 12
来 自:邯郸
等 级:火箭侠
帖 子:1250
专家分:3538
注 册:2011-4-7
收藏
得分:0 
运行的时候出现啥问题?
2011-06-23 17:46
郭胖
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2011-5-5
收藏
得分:0 
回复 2楼 voidx
Unhandled exception at 0x00032d17 in BT seed.exe:0xc00000FD:Stack overflow
2011-06-23 18:56
voidx
Rank: 12Rank: 12Rank: 12
来 自:邯郸
等 级:火箭侠
帖 子:1250
专家分:3538
注 册:2011-4-7
收藏
得分:0 
这不是栈溢出么?
2011-06-23 19:05
郭胖
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2011-5-5
收藏
得分:0 
回复 4楼 voidx
那怎么处理啊?
2011-06-23 23:36
cosdos
Rank: 9Rank: 9Rank: 9
来 自:ShangHai
等 级:蜘蛛侠
威 望:6
帖 子:2109
专家分:1385
注 册:2007-6-19
收藏
得分:0 
代码收下看看

—>〉Sun〈<—
2011-06-24 20:32
cosdos
Rank: 9Rank: 9Rank: 9
来 自:ShangHai
等 级:蜘蛛侠
威 望:6
帖 子:2109
专家分:1385
注 册:2007-6-19
收藏
得分:0 
#include "sha1.h"   // 没有这个文件

—>〉Sun〈<—
2011-06-24 20:36
快速回复:bt种子文件生成
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.041637 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved