-4

How is the following code in Python in C#?

I need to get the hash generated by this code to send to http://en.thesubdb.com/api/ to get a subititle, but my code is in C #.

def get_hash(name):
    readsize = 64 * 1024
    with open(name, 'rb') as f:
        size = os.path.getsize(name)
        data = f.read(readsize)
        f.seek(-readsize, os.SEEK_END)
        data += f.read(readsize)
    return hashlib.md5(data).hexdigest()
  • Sorry for my bad English.
5
  • If you're not willing to translate the python code to C# yourself, maybe you could just add ironpython to your project and invoke that python code through that... Jul 5, 2016 at 23:25
  • Welcome to Stack Overflow! Stack Overflow is a Question and Answer site, not a code translation service. Please try to translate the code yourself first, then come to us when you are stuck, making sure to show us what you have tried, preferably in the form of a Minimal, Complete, Verifiable Example. Jul 5, 2016 at 23:33
  • I'm trying to hours, until I decided to put here.
    – Paulo Leal
    Jul 5, 2016 at 23:38
  • Looks like size is never used. And with just a quick look I think it reads the file backwards then do a md5 of that.
    – BrunoLM
    Jul 5, 2016 at 23:41
  • I saw, but dont know how python work imagined an abstraction.
    – Paulo Leal
    Jul 6, 2016 at 0:05

1 Answer 1

0
public static string GetHash( string caminho ) {
        const int bytesToRead = 64 * 1024;
        byte[] buffer = new byte[bytesToRead * 2];
        MD5 md5 = new MD5CryptoServiceProvider( );
        using( var md52 = MD5.Create( ) ) {
            using( var stream = new FileStream( caminho, FileMode.Open, FileAccess.Read ) ) {
                for( int i = 0; i < bytesToRead; i++ ) {
                    buffer[i] = (byte)stream.ReadByte( );
                }

                stream.Seek( -bytesToRead, SeekOrigin.End );

                for( int i = bytesToRead; i < bytesToRead * 2; i++ ) {
                    buffer[i] = (byte)stream.ReadByte( );
                }
                return BitConverter.ToString( md52.ComputeHash( buffer ) ).Replace( "-", "" ).ToLower( );
            }
        }          
    }

Reading byte by byte I got. For those who need that is code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.