简体   繁体   中英

Node Server Running on Ubuntu that needs to execute a C# application - how?

I have a very simple C# application that was written by a previous person. It uses the MD5CryptoServiceProvider to compute hash using some seed strings to generate a random code.

I have a Node.js server running that I'd like to execute this app and consume the output.

I cannot figure out how to run C# from Ubuntu and I cannot find an MD5 library that comes up with the same answer.

Here is the entirety of the function:

{

  MD5 md5 = new MD5CryptoServiceProvider();

  byte[] encodeBytes = System.Text.Encoding.UTF8.GetBytes(seed1+ seed2+ seed3);

  byte[] output = md5.ComputeHash(encodeBytes);

  string result = string.Format("{0:D}{1:D}{2:D}{3:D}{4:D}{5:D}",

                                ((output[0] + output[1]) % 10), ((output[2] + output[3] + output[4]) % 10),

                                ((output[5] + output[6]) % 10), ((output[7] + output[8] + output[9]) % 10),

                                ((output[10] + output[11] + output[12]) % 10), ((output[13] + output[14] + output[15]) % 10));

  return result;

}

Is it possible for me to modify this C# script to run as a child process or get the same answer using a C++ script, which I have gotten to run with a Node server previously.

Thank you!!

You should just convert your code to Javascript:

function generateCode() {

  let seed1 = 'abc';
  let seed2 = 'def';
  let seed3 = 'ghi';
  let hashed = md5(seed1 + seed2 + seed3);
  let values = [];

  for (let i = 0; i < hashed.length; i += 2) {
    values.push(parseInt('0x' + hashed.substr(i, 2)));
  }

  let codeParts = [((values[0] + values[1]) % 10), ((values[2] + values[3] + values[4]) % 10),((values[5] + values[6]) % 10), ((values[7] + values[8] + values[9]) % 10),((values[10] + values[11] + values[12]) % 10), ((values[13] + values[14] + values[15]) % 10)];
  let code = codeParts.join('');
    alert(code);
}

generateCode();

I used the md5 implementation from here but any that returns the hex as a string should work.

Javascript sample

C# for comparison (same seed)

You could of course simplify this, but this serves as a demonstration of how easy it is to simply convert the code.

You can run C# files on Ubuntu by using mono and mcs, i think they work really well. Here is a tuturial link on how to do it.

TL;DR: First you will need to install Monodevelop. This can be done with the following commands:

sudo apt-add-repository ppa:directhex/ppa  
sudo apt-get update  
sudo apt-get install monodevelop 

After installing mono you will need to install mcs:

sudo apt-get install mcs 

When both are installed correctly you need to make a .cs file (a C# file). You can compile the program with mcs ( mcs <filename.cs> ). Then use mono to execute the generated exe file: mono <filename.exe> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM