Suppose you have an existing hash g84t5tw73y487tb38wo4bq8o34q384o7nfw3q434hqa which was created from the original string dont downvote my stupid question
Now I timestamp this hash like this (in JS/pseudo-code):
var hash = 'g84t5tw73y487tb38wo4bq8o34q384o7nfw3q434hqa';
var today= new Date(); // 2017-10-19
var timestamped = hash + today;
var new_hash = SHA256(timestamped);
// new_hash is 34t346tf3847tr8qrot3r8q248rtbrq4brtqti4t
If I wanted to verify my original string I can do:
var verified = goodHash('dont downvote my stupid question',hash); // true
If I wanted to verify the timestamped version I can do:
var original_hash = 'g84t5tw73y487tb38wo4bq8o34q384o7nfw3q434hqa';
var today = '2017-10-19';
var verified = goodHash(original_hash+today, timestamped_hash); // true
But if I tried to verify the original string against the timestamp, I CANT do:
var today = '2017-10-19';
var verified = goodHash('dont downvote my stupid question'+today, timestamped_hash); // FALSE
Now suppose this original string is hashed and timestamped over and over again for n iterations.
I would only ever be able to verify the n-1th timestamp, provided I have the n-1th hash.
But what if I have the original string dont downvote my stupid question and want to verify any ith timestamp, where 0 < i < n.
Basically, I want to verify whether a string that only I should have knowledge of, has been timestamped with a given date, regardless of how many times it may have been timestamped and without increasing the length of the string (too much - although any increase in length would approach infinity as n grows).
Is this even possible? Can a hash even contain all this information?
Let's look at the mathematics involved here:
First, you have a input string s and a sequence t of timestamps. I will use t[i] to denote the ith timestamp. Your repeated hashing is a recurrence relation:
f(i) = hash(f(t[i-1]) + t[i])
Where + denotes string concatenation. Now we want to determine if there is a closed formula F(x) which will calculate the ith hash with less time-complexity than evaluating the recurrence relation f(i).
One way to accomplish this is to find a string x(i) with the same hash as f(t[i-1]) + t[i]. For a good hashing algorithm, these collisions are exceedingly rare. My gut instinct is that finding such a string (other than f(t[i-1]) + t[i] itself) is more difficult than simply calculating directly from the recurrence relation.
Related
The objective is the get the object from Realm that contains the highest numeric value in one of its properties. The object has a persons name (a string) and a person_id (also a string).
PersonClass: Object {
#objc dynamic var name = ""
#objc dynamic var person_id = ""
}
the person_id can be a number, string or a combination. For this filter, all strings that do not contain only a number should be ignored. The table may look like this
name person_id
Henry 0000
Leroy test
Frank 3333
Steve a123
and the result should be
Henry 0000
Frank 3333 <- .last or the highest
For a simple example, let's take this array
let arrayOfWords = ["thing", "1", "stuff", "2"]
and some code to get the string "1" and "2".
let swiftResults = arrayOfWords.compactMap { Int($0) } //knowing that Int has limitations
swiftResults.forEach { print($0) } //note we can use .last to get the last one
While there is solution by getting all Realm objects and then applying a Swift filter, the problem is there could be thousands of persons and as soon as the results object is manipulated as a Swift enumerated object (like an array), it not only breaks the connection with Realm (losing the objects live updating ability) but they are are no longer lazily loaded, taking up memory and possibly overwhelming the device.
Since realm queries do not support regex, you'd have to check that no letter is contained in person_id and get the max value using #max.
let regexAnythingButDigits = "[^0-9]"
let maxNumericID = realm.objects(PersonClass.self).filter("person_id NOT CONTAINS[c] 'a' person_id NOT CONTAINS[c] 'b' AND ..[all letters in between]... AND person_id NOT CONTAINS[c] 'z' AND person_id.#max", regexAnythingButDigits)
Based on the insight provided in the answer by rs7, here's a coding solution that meets the criteria in the question.
TLDR: we build a compound AND predicate that filters Realm for all strings that have no alpha characters. i.e. get all strings that do not have 'a' AND 'b' AND 'c' etc.
This is important because there could be thousands of objects and while filtering using pure Swift is super simple code-wise, doing that goes around the lazy-loading characteristic of Realm and the entire dataset would need to be loaded into the array for filtering, which could overwhelm the device.
This solution keeps the Realm objects lazy avoiding that issue.
let charStringToOmit = "abcdefghijklmnopqrstuvwxyz" //the invalid chars
let charsOmitArray = Array(charStringToOmit) //make an array of Char objects
var predicateArray = [NSPredicate]() //the array of predicates
//iterate over the array of char objects
for char in charsOmitArray {
let c = String(char) //make each char a string
let pred = NSPredicate(format: "!(person_id CONTAINS[cd] %#)", c) //craft a predicate
predicateArray.append(pred) //append the predicate to the array
}
//craft compound AND predicate
let compound = NSCompoundPredicate(andPredicateWithSubpredicates: predicateArray)
let realm = try! Realm()
let results = realm.objects(PersonClass.self).filter(compound).sorted(byKeyPath: "person_id")
if let lastPerson = results.last {
print(lastPerson.person_id)
}
I simplified the initial dataset provided for this example and limited it to only have a-z and 0-9 characters but that could be expanded on.
I wrote this function to generate random unique id's for my test cases:
func uuid(t *testing.T) string {
uidCounterLock.Lock()
defer uidCounterLock.Unlock()
uidCounter++
//return "[" + t.Name() + "|" + strconv.FormatInt(uidCounter, 10) + "]"
return "[" + t.Name() + "|" + string(uidCounter) + "]"
}
var uidCounter int64 = 1
var uidCounterLock sync.Mutex
In order to test it, I generate a bunch of values from it in different goroutines, send them to the main thread, which puts the result in a map[string]int by doing map[v] = map[v] + 1. There is no concurrent access to this map, it's private to the main thread.
var seen = make(map[string]int)
for v := range ch {
seen[v] = seen[v] + 1
if count := seen[v]; count > 1 {
fmt.Printf("Generated the same uuid %d times: %#v\n", count, v)
}
}
When I just cast the uidCounter to a string, I get a ton of collisions on a single key. When I use strconv.FormatInt, I get no collisions at all.
When I say a ton, I mean I just got 1115919 collisions for the value [TestUuidIsUnique|�] out of 2227980 generated values, i.e. 50% of the values collide on the same key. The values are not equal. I do always get the same number of collisions for the same source code, so at least it's somewhat deterministic, i.e. probably not related to race conditions.
I'm not surprised integer overflow in a rune would be an issue, but I'm nowhere near 2^31, and that wouldn't explain why the map thinks 50% of the values have the same key. Also, I wouldn't expect a hash collision to impact correctness, just performance, since I can iterate over the keys in a map, so the values are stored there somewhere.
In the output, all runes printed are 0xEFBFBD. It's the same number of bits as the highest valid unicode code point, but that doesn't really match either.
Generated the same uuid 2 times: "[TestUuidIsUnique|�]"
Generated the same uuid 3 times: "[TestUuidIsUnique|�]"
Generated the same uuid 4 times: "[TestUuidIsUnique|�]"
Generated the same uuid 5 times: "[TestUuidIsUnique|�]"
...
Generated the same uuid 2047 times: "[TestUuidIsUnique|�]"
Generated the same uuid 2048 times: "[TestUuidIsUnique|�]"
Generated the same uuid 2049 times: "[TestUuidIsUnique|�]"
...
What's going on here? Did the go authors assume that hash(a) == hash(b) implies a == b for strings? Or am I just missing something silly? go test -race isn't complaining either.
I'm on macOS 10.13.2, and go version go1.9.2 darwin/amd64.
String conversion of an invalid rune returns a string containing the unicode replacement character: "�".
Use the strconv package to convert an integer to text.
Im trying to find out what kind of hash encryption this is in javascipt:
function L(j) {
var h = a.pbkey,
g = a.rndnb;
if (typeof f != "undefined") {
var e = f.PBK(h),
d = f.STBUNC(j),
i = f.STBA(g),
c = f.ENC(d, e, i),
b = f.BAT64(c);
return b
} else return ""
}
It hash a phonenumber.Ex: +79645531974
pbkey:'e=010001;m=d06889ab9ea3c9ce9d2e92559d6b2f043ef873f0cc9f858745bc6784d1d9a98f8d354e061d25fad9c3a741c57626d0d65de01eb03584a5af1f579b9f9834a60b628756ad636ec1d3129e87e0c7274670d9ca615f12fe3424e9da362f7f8cce8dfe61d79f5ec68dffe1f3ddcf5e20e1bf07349ee6c747a59b09d6420d131899f93dfaeacd76d6a684cda8ac99e7f87e17507235a2b59b84f56d8a3d4ecdd8259b3c7d892758d69a48930b591e66cc6d88d20a3de9360be30c8a94084a2753c92e0cfd1c94868c90109cd176855bba96cd3e73f34442ddfd256da7f1d1e48fbf265ad2f2caeebe4990ca5638b90b6c3fafa8c015a09947e3f03defc51e231a2f2d'
rndnb:'7fc1cdfea47d0057bbb33176ce73a376f9319d4e221d84807d74ff2f859b510b9fd132e577ed207d96b1d11e57500bff93efe97842248bcbe39527592797b7e3a821110ae61c3da67c2773bcb634c53357fb230ef95297d20c37d256aa8bd75bea315f2d
Result: 4LW/+zyiBBgDExOLPLafO9T/GG3guycSMK3uz16qFcXWgvo1KAF8VrbGrxAE91Mvk6qUDkX8c9ha7urDB41XDAhciBbj2VzE48WXjB/A6gI6n7qcTwkNTPT0Qly1EFRtTF44xTbPEld/OviYhD2OolumbtL42wtnyw1oh4/2v2SyAqARdGJizRhd1UFpWW+OUIcF3eyhKS1R+TDorsOoM/bJQzR6CTSyLysfPJL8ldjG0Ujevac7dT+WvaXFmP3qlsReMP/FSLjs7xixCAA/VrxIRUragoIOf2cptilop5zJNY26DO/iEhUUU7n8ANayrqthplS3v624XR24iM22bg==
The code suggests that it uses 2048 bit RSA encryption (with public exponent 65537) and a randomized padding scheme.
But with only this code it is impossible to solve the whole thing. We need to know what the PBK(), STBUNC(), STBA(), ENC(), BAT64() functions do, otherwise we cannot definitively say anything about what is done here.
However, the input parameters give some suggestions. The pbkey parameter suggests that the encryption is "public-key" based and uses an exponent e = 65537 (a commonly used RSA public exponent). Then it's an easy guess that m stands for "modulus". We could be dealing with either RSA or some logarithm-group crypto (e.g. ElGamal). To get more information we could check if this modulus m is a prime number, turns out it's not. So logarithm-group crypto is off the table and so we are probably dealing with RSA. Notice how the modulus and the output are each 2048 bits, so this checks out.
But we're not done. We also have the rndnb value, which probably means "random number". This suggests that the encryption is not textbook RSA (thank god), but uses some kind of randomization. Possible it uses some kind of standard padding scheme like OAEP.
To get more details we would really need to get the bodies of the functions that are use.
I want to store serialized objects (or whatever) in a key/value cache.
Now I do something like this :
public string getValue(int param1, string param2, etc )
{
string key = param1+"_"+param2+"_"+etc;
string tmp = getFromCache();
if (tmp == null)
{
tmp = getFromAnotherPlace();
addToCache( key, tmp);
}
return tmp;
}
I think it can be awkward. How can I design the key?
if i understood the question, i think the simplest and smartest way to make a key is to use an unidirectional hash function as MD5, SHA1 ecc...
At least two reason for doing this:
The resulting key is unique for sure!(actually both MD5 and SHA1 have been cracked (= )
The resulting key has a fixed lenght!
You have to give your object as argument of the function and you have your unique key.
I don t know very much c# but i am quite sure you can find an unidirectional hash function builted-in.
First of all your key seems to be composed out of a lot of characters. Keep in mind that the key name also occupies memory (1byte / char) so try to keep it as short as possible. I've seen situations where the key name was larger than the value, which can happen if you have cases where you store an empty array or an empty value.
The key structure. I guess from your example that the object you want to store is identified by the params (one being the item id maybe, or maybe filters for a search [...]). Start with a prefix. The prefix should be the name of the object class (or a simplified name depicting the object in general).
Most of the time, keys will have a prefix + identifier. In your example you have multiple identifiers. If one of them is a unique id, go with only prefix + id and it should be enough.
If the object is large and you don't always use all of it then change your strategy to a multiple key storage. Use one main key for storing the most common values, or for storing the components of the object, values of which are stored in separate keys. Make use of pipes and get the whole object in one connection using one "multiple" query :
mainKey = prefix + objectId;
object = getFromCache(mainKey);
startCachePipeline();
foreach (object[properties] as property) {
object->property = getFromCache(prefix + objectId + property);
}
endCachePipeline();
The structure for an example "Person" object would then be something like :
person_33 = array(
properties => array(age, height, weight)
);
person_33_age = 28;
person_33_height = 6;
person_33_weight = 150;
Memcached uses memory most efficient when objects stored inside are of similar sizes. The bigger the size difference between objects (not talking about 1 lost big object or singular cases, although memory gets wasted then as well) the more wasted memory.
Hope it helps!
private function getPercentage(max:Number, value:Number):int
{
return Number((value*100) / max);
}
I call the above function to assign a percentage to an object.
var max:Number = findMax();
p.percentage = getPercentage(max, p.value);
Assume that p is some object with percentage defined as
public var percentage:Number;
When I put a breakpoint and check for the value returned in getPercentage it will something like 1.22343342322 but when I assign it to p.percentage it automatically becomes 1, losing the precision.
How do I handle this kind of a situation?
It says in the LiveDocs
To store a floating-point number,
include a decimal point in the number.
If you omit a decimal point, the
number will be stored as an integer.
But how do I do that? Any insight to this problem is highly appreciated.
Your method getPercentage() returns int. Change it to Number.