Hashing a byte array [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have an input byte array which is huge in size ( > 8000 bytes). I need to store this byte array in DB, and fetch later for further operation.
The problem is, this byte array column is unique. Assume that if I need to retrieve the entire table information on this byte array column. It becomes extremely costly operation when it comes to byte comparison. So thought of storing the Hash Value of this byte array just to make the comparison operation easier.
Just wanted to know if hash value which is generated from the byte array will be unique OR is there any other way to achieve this.

If the size (in bytes) of the hash is smaller than 8000, this is not possible. After all, there are 256 ^ 8000 possible inputs, so there must be at least that many possible outputs if the hash function needs to be unique.
As the default C# hashcode returns an int, which (depending on your architecture) is 32 or 64 bits (so 4 or 8 bytes), this is not even close to be possible with the default hash function. (Of course, you could write your own hash function, but that's quite pointless.)

Related

Convert byte array to integer with lower and upper bound [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have a method that pseudo-randomly encrypts a byte array. I would like to convert the encrypted byte-array to a C# int (4 bytes), while observing a user-specified lower and upper bound (e.g. give me a number between 1 and 10)
What is the most secure and performant way of achieving this?
You could use System.ByteConverter.ToInt32(arr, start_index).
From your description of the array, the start index would likely be zero.
I am not sure what you mean by upper and lower bound, but if you mean you want a random number, you could use a variety of functions to get a value in between the two numbers. If you have semi-uniform distribution, the modulus function would work nicely. In that case, your random number would simply be lowerBound + (System.ByteConverter.ToInt32(arr, start_index) % (upperBound - lowerBound)).

How to store a bit in C# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have the following code:
void MyBitMaker(int inputNumber)
{
Console.WriteLine(inputNumber << 1);
//TODO
//Bit bitHolder = inputNumber << 1;
//InitMico(ref bitHolder);
}
There is not a type for storing bits in C#. Is there any way to store bits in a variable.
I am programming a micro controller using C#, It uses the bits that are coming from a web service, fetches them and sends them to the controller to open, close, sleep and such things using an interface to change the input data to appropriate bits. My problem is that the micro has just 16 bytes memory in ram and I can not store more than two bytes, the micro should store the history of previous actions (this is extra, maybe, jargon). I have low space and need this small unit. I searched a lot and did not find anything. currently I am using bit operators using a class that I myself have implemented but it's not efficient at all because of using bit operations. I was wondering if someone has faced something that can help me.
The smallest addressable unit is a byte, so use that, or a bool which is still 8 bits, but only has two possible values to be set.
You can't reference a bit, so that would be useless anyway. If you need to address a specific position in a byte, you can pass along the position. Still, you can only change the bit's value by setting the entire byte.
I just want to store bits
Trivial solution: bool array.
If it really needed to store the bits in a compacted form, you can use the BitArray type, which uses an int array internally. You can index it similarly to a normal array:
var myBits = new BitArray(20); // initialize for 20 bits (1 int will be stored internally)
myBits[5] = true; // similar to this: myInt |= 1 << 5;

C# sbyte is written to the file as an unsigned byte [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Using the BinaryWriter I write an sbyte variable to a file but it gets written to the file as unsigned and not signed as it should be.
sbyte a = (sbyte)image.pixelData[i + 0];
bw.Write(a);
For example, the above code writes values ranging from 0x00 to 0xFF. (These are the values I see in a hex editor.)
You are demonstrating a fundamental misunderstanding of how data is stored in memory, files, etc.
All computer data is in binary form.
The different data types determine how the data is treated, how calculations are performed, how values are formatted, etc.
If you write a signed value to a file, it is always written using binary form (the only format a computer understands). If you read that data with a hex editor, then the hex editor will translate the data using whatever translation it considers to be appropriate.
If you write data to a file as a signed byte, and then you read that same data back as a signed byte, then the data will be the same as the data written. You should expect no more, and no less.
(Note: If you use the hex editor I wrote (Cygnus Hex Editor), you can inspect the data using any data type. In that case, you can have it appear in the format you expect. Otherwise, the hex editor is converting it to hex, or whatever, which tells you nothing about how the data is stored in the file.)

Splitting a 200 bit hexadecimal bitmask [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a bitmask of 200 bit stored as a hexadecimal value.
In order to apply the & bit operator, I have to first convert the hex to an integer but 200 bit is too big for a uint64 so my question is : how do I split my bitmask in 4 different hexadecimal value without loosing data?
So that I can also split my 200 bit data and then compare every chunk of data with the corresponding chunk of bitmask without altering the result.
You can use the BigInteger from System.Numerics (it's a separate assembly):
BigInteger bi = BigInteger.Parse("01ABC000000000000000000000000000000000", System.Globalization.NumberStyles.HexNumber);
VERY IMPORTANT: prepend a "0" before the hex number! (because BigInteger.Parse("F", NumberStyles.HexNumber) == -1, while BigInteger.Parse("0F", NumberStyles.HexNumber) == 15
BigInteger implement the "classical" logical operators (&, |, ^)
Requires .NET 4.0
The most efficient way of achieving this is writing a class that can store and do binary operations on 200bits of data, have strings as input, etc.

C# 64 characters key [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I don't know C# but I have to do a update to a function that send a key to be encrypted.
I need to send a key with 64 characters, but the function that I have here only accepts 32 characters.
The function that initialize this process is:
Byte[] kkey = System.Text.Encoding.UTF8.GetBytes(sEncKey);
What I have to do for this accept a 64 characteres key?
The code you have included creates an array of type Byte which includes a number of elements matching whatever number of characters you provide in sEncKey.
It is my understanding that you are passing kkey to some function that will perform the encryption, and that this function only accepts a byte array of size 32. If this is correct, then you can't automatically increase the key size to 64 just by sending a longer key - it is the function you will have to change first, not it`s input.
Sound's like you're trying to increase security by using a larger key without replacing the lock first. Get a bigger lock first, then you can use a bigger key.
Also, if you need more help or guidance with this, you will need to provide info about the function, as well as some relevant code.

Categories