Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8964759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:39:41+00:00 2026-06-15T16:39:41+00:00

I don’t know a lot about torrents, at least not enough to understand how

  • 0

I don’t know a lot about torrents, at least not enough to understand how certain websites can offer both a normal download link and a torrent link to download a file uploaded by a user.

Is generating a torrent link something common and simple to achieve. Would I need a server installation?

I’ve made an ugly C# implementation from a Java source, and to make sure some of my encoded variables were correct, I used NBEncode from Lars Warholm.

    // There are 'args' because I'm using it from command-line. (arg0 is an option not used here)
    // Source file
    args[1] = Directory.GetCurrentDirectory() + args[1];
    // Name to give to the torrent file
    args[2] = Directory.GetCurrentDirectory() + args[2];

    var inFileStream = new FileStream(args[1], FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
    var filename = args[2];

    //BEncoding with NBEencode
    var transform = new BObjectTransform();
    MemoryStream s = new MemoryStream();
    OSS.NBEncode.Entities.BDictionary bod = new OSS.NBEncode.Entities.BDictionary();
    OSS.NBEncode.Entities.BDictionary meta = new OSS.NBEncode.Entities.BDictionary();

    // Preparing the first part of the file by creating BEncoded objects
    string announceURL = "https://www.mysite.com/announce";
    int pieceLength = 512 * 1024;
    bod.Value.Add(new BByteString("name"), new OSS.NBEncode.Entities.BByteString(filename));
    bod.Value.Add(new BByteString("length"), new OSS.NBEncode.Entities.BInteger(inFileStream.Length));
    bod.Value.Add(new BByteString("piece length"), new OSS.NBEncode.Entities.BInteger(pieceLength));
    bod.Value.Add(new BByteString("pieces"), new BByteString(""));
    meta.Value.Add(new BByteString("announce"), new BByteString(announceURL));
    meta.Value.Add(new BByteString("info"), bod);
    byte[] pieces = hashPieces(args[1], pieceLength);
    transform.EncodeObject(meta, s);
    s.Close();

    // Notice that we finish with a dictionary entry of "pieces" with an empty string.
    byte[] trs = s.ToArray();
    s.Close();
    inFileStream.Close();

    // I don't know how to write array of bytes using NBEncode library, so let's continue manually

    // All data has been written a MemoryStreamp, except the byte array with the hash info about each parts of the file

    Stream st = new FileStream(filename + ".torrent", FileMode.Create);
    BinaryWriter bw = new BinaryWriter(st);

    // Let's write these Bencoded variables to the torrent file:
    // The -4 is there to skip the current end of the file created by NBEncode

    for (int i = 0; i < trs.Length - 4; i++)
    {
        bw.BaseStream.WriteByte(trs[i]);
    }

    // We'll add the length of the pieces SHA1 hashes:
    var bt = stringToBytes(pieces.Length.ToString() + ":");

    // Then we'll close the Bencoded dictionary with 'ee'
    var bu = stringToBytes("ee");

    // Let's append this to the end of the file.
    foreach (byte b in bt)
    {
        bw.BaseStream.WriteByte(b);
    }
    foreach (byte b in pieces)
    {
        bw.BaseStream.WriteByte(b);
    }
    foreach (byte b in bu)
    {
        bw.BaseStream.WriteByte(b);
    }
    bw.Close();
    st.Close();                    

    // That's it.
}

Functions used:

    private static byte[] stringToBytes(String str)
    {
        System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
        Byte[] bytes = encoding.GetBytes(str);
        return bytes;
    }

   private static byte[] hashPieces(string file, int pieceLength)
    {

        SHA1 sha1 = new SHA1CryptoServiceProvider();

        StreamReader inn = new StreamReader(file);
        MemoryStream pieces = new MemoryStream();
        byte[] bytes = new byte[pieceLength];
        byte[] digest = new byte[20];
        int pieceByteCount = 0, readCount = inn.BaseStream.Read(bytes, 0, pieceLength);
        while (readCount != 0)
        {
            pieceByteCount += readCount;
            digest = sha1.ComputeHash(bytes, 0, readCount);
            if (pieceByteCount == pieceLength)
            {
                pieceByteCount = 0;
                foreach (byte b in digest)
                {
                    pieces.WriteByte(b);
                }
            }
            readCount = inn.BaseStream.Read(bytes, 0, pieceLength - pieceByteCount);
        }
        inn.Close();


        if (pieceByteCount > 0)
            foreach (byte b in digest)
            {
                pieces.WriteByte(b);
            }
        return pieces.ToArray();
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:39:42+00:00Added an answer on June 15, 2026 at 4:39 pm

    It depends on how you’re trying to create it. If you run a website, and want to generate torrent files from uploaded files, then you’ll obviously need server-side code.

    Generating a torrent file involves: adding the files you want to the torrent, and adding tracker info. Some popular trackers are:

    1. http://open.tracker.thepiratebay.org/announce
    2. http://www.torrent-downloads.to:2710/announce

    To create the .torrent file, you’ll have to read the about the format of the file. A piece of Java that generates .torrent files is given at https://stackoverflow.com/a/2033298/384155

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Don't know a whole lot about streams. Why does the first version work using
don't know if the title describes anything about what I'm trying to say but
Don't know why but font is not displaying.Please help. CSS(in css folder): style.css: @font-face
Don't know why people do not practice AJAX implementation for authentication systems. Is it
Don't know why this doesn't work. I can't do implicit animation for a newly
Don't know if it matters, but the msg.d is about 300 rows long. I
Don't know if I'm over-thinking this or not.. but I'm trying to be able
Don't know much about encryption... Say I'm preparing a SAML request to submit to
(Don't care about the version. IE or not IE.)
Don't know why I can't reply to people on here with only a small

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.