is it possible that hash function produces the same hash value for two different inputs? [closed] - security

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 12 months ago.
Improve this question
is it possible that hash function produces the same hash value for two different inputs ? I am not mistaken I have read that hash function is one way encryption, so we cannot go back to the input using the result, and read that even if we change a bit of the ducoment that is directed to be hadhed will give another hash value, so how does collusion happen? and how is it possible for a hash function to produce the same value for two different inputs?

Yes. The range of any hash function is limited by the number of bits. For example, if we were to use a hash function outputting 8 bits, then we only have 256 possible outputs; so if we have more than 256 possible inputs then we are guaranteed to have a collision.
If we use a hash function with a larger range then it becomes much less likely to have a collision; but cannot make it impossible because our inputs are unlimited.
The existence of collisions is part of the reason why these functions are one way: a given output could have been generated from an unlimited number of different inputs.
We should also use a good cryptographic hash function to help avoid collisions from similar input.
You may want to look at the "SHAttered" example of 2 different PDF files that have the same SHA1 hash.

Related

finding the maximum in a HashSet<u32> in rust? [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 last year.
The community reviewed whether to reopen this question last year and left it closed:
Original close reason(s) were not resolved
Improve this question
how can i find the maximum or minimum value in a HashSet without looping through all the elements in it? Is there any function or one liner way to do so ?
You can do something like this:
let hs: HashSet<u32> = HashSet::from_iter(vec![54, 23, 55, 6, 3, 100]);
let min_value = *hs.iter().min().unwrap();
let max_value = *hs.iter().max().unwrap();
That way you won't have to loop yourself, but internally that's exactly what is going to happen. It's not possible to find the min or max value in a hash set (or a hash map, for that matter), without checking all the values.
BTW there is another (relatively common) set type, BTreeSet, which keeps its values ordered. That would allow you to simply get the first or the last element (via iter().next() or iter().next_back()) and you've got you min/max value w/o any extra work. Performance-wise that would be much faster of course.

Diff between constant and variable [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
What exactly is the difference between a constant and a variable. Can constants be understood as values that can be assigned to variables in a program
You basically have the right idea. The correct idea is a bit hard to give with the small amount of information you give in your question.
"constant" is a bit vague. The name is used to refer to literals, symbolic constants, constant expressions, immutable variables ...
Anyway, the answer depends strongly on what language you're using and what context you've heard the term "constant" in.
For example, in the C programming language, most symbolic constants do not exist at runtime. They are simply names that are replaced by their actual literal values as the first step before compilation.
In other languages, constants are named variables that are saved into the built program that contain a value that can't be changed, and can be listed or so.
Wait, constants are sometimes variables?
Well, the terms "constant" and "variable" are kind of vague concepts that are sometimes used wrongly, and don't have a straight translation into machine code.
At the core, there is just memory. And memory contains data. Constants are usually parts of the memory that are just loaded from disk for you by the operating system together with your compiled code, and then your code can read them. Variables are parts of the memory for which "gaps" in memory are set aside by the system, and then your code can put values into it or read it.
That's why it's a bit hard to offer a concise definition of a variable and a constant. It depends on what level of the computer you are looking at it, through which language.
In most languages, a symbolic constant is simply a more convenient name you can use in your code to refer to a fixed number or other literal value. A name whose value you can change in one central location before you compile your code, and all other places that use the symbolic name automatically pick up the value.
Variables are boxes into which you can put any value.
So you're basically right. But there can be more to the story depending on what language you're using.
The reason for symbolic constants is mostly to make your code more readable. Instead of
leftCoordinate = 16 + 20 + 4
you can write
leftCoordinate = LEFT_MARGIN + SIDEBAR_WIDTH + LINE_WIDTH
and suddenly it is much more obvious which of these numbers you have to change to change the right part. Also, you can use them to make sure two numbers always match. Like, elsewhere in your program, you may have the code that draws the "line" mentioned above, and just do
setLineWidth(LINE_WIDTH)
drawLine(LEFT_MARGIN + SIDEBAR_WIDTH, 0, LEFT_MARGIN + SIDEBAR_WIDTH, 100)
And if you ever decide you want a thinner line, you just change the constant's value, and all your code magically updates, and you just need to recompile.

How to use a float in Java Card? [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 4 years ago.
Improve this question
I need your help: does anyone know how I could present a float in Java Card?
The floating point number I need is 0.9. I've heard that I need to use a float point or something like this but I am not really sure.
Just like most Java Card implementations do not feature a 32-bit integer, they do not contain floating point arithmetic either. This however even goes much deeper: the compiled byte code for floating point is not even supported. So in the end you will have to do this yourself or look for vendor support. Note that most smart card CPU cores won't do floating point either, so it would have to be emulated using integer arithmetic.
If you require arithmetic on real numbers for money calculations or similar then you'd best look into fixed-point arithmetic. One trick is to simply perform calculations where each value is multiplied with 100, i.e. do calculations using cents. So then 0.90 times 10 would become 90 times 10. Then - at the terminal - you can simply re-insert the comma.
If you want to do integer calculations (optional but usually not supported), check my X-mas special answer here ... probably a contender for most complex answer on SO when it comes to code. This way you can do 32 bit calculations which you may need to handle any kind of precision (of ~9 decimal digits instead of ~4 that you get with shorts).
If you only need to store a float then simply encode the floating point to bytes, e.g. using DataOutputStream and store the resulting bytes. Or encode a packed BCD and use a byte to represent where the comma needs to be.

F# when should you use tuple and when should you use struct? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
What are the underlying differences between F# tuples and structs. They can both hold multiple variables, but I just want to know when one is used over the other.
To be more specific, I am trying to pass a bunch of parameters through a bunch of functions with a new variable being accumulated each time. For example, I start with param1 and pass it into func1 which returns (param1,param2). This tuple (or struct) then gets passed to func2 which returns (param1,param2,param3), and so on.
My current thoughts are this: with a tuple, I can always hold just the right amount of arguments, but I give up a uniform format for the data, and in the end, I would have to pack and repack a tuple of about 10 elements. With a struct, I have the advantage of uniformity of the parameters, but the problems is, I would have to null specify the parameters in the beginning.
In F#, tuples are represented using Tuple<T1, T2> which is a reference type. On the other hand, structures are value types and so they are allocated on stack rather than on the heap (which may sometimes be faster). So my general rules are:
Tuples have nice syntactic support in F#, so use tuples by default because they make your code nicer. In most cases, the performance is similar and you do not need to worry about it (it depends on the use - tuples are not always slower).
When your tuples get more complicated (say, more than 3 elements), it makes sense to use a type with named members (like a record or an object type).
When allocating a large array of tuples or structs, it is better to use structs. You can either define your own struct or use standard .NET structs like KeyValuePair<K, V>.
To give an anecdotal evidence, in Deedle we are using structs for the internals (time-series is stored as an array of structs), but not for the public API, which uses tuples.

What's the difference between collision resistance and preimage resistance? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
For hash function, what's the difference for collision protection and preimage protection?
from wikipedia: http://en.wikipedia.org/wiki/Cryptographic_hash_function
Properties
Most cryptographic hash functions are designed to take a string of any
length as input and produce a fixed-length hash value. A cryptographic
hash function must be able to withstand all known types of
cryptanalytic attack. As a minimum, it must have the following
properties:
Preimage resistance Given a hash h it should be difficult to find any message m such that h = hash(m). This concept is related to that of one-way
function. Functions that lack this property are vulnerable to preimage
attacks.
Second-preimage resistance Given an input m1 it should be difficult to find another input m2 — where m1 != m2 — such that hash( m1 ) = hash( m2 ). This property is
sometimes referred to as weak collision resistance, and functions that
lack this property are vulnerable to second-preimage attacks.
Collision resistance It should be difficult to find two different messages m1 and m2 such that
hash( m1 ) = hash( m2 ). Such a pair is called a cryptographic hash
collision. This property is sometimes referred to as strong collision
resistance. It requires a hash value at least twice as long as that
required for preimage-resistance, otherwise collisions may be found by
a birthday attack.

Resources