incremental id as alphanumeric code - mysql

I need to show id as alphanumeric code url friendly and not more than 8 chars. And this code shouldn`t be stored, but should be decodable to the original id value.
Something like ENCODE/DECODE pair, but url friendly. How can I do this?

One possibility is to use a 64-bit hash code. Algorithm in this stackoverflow post:
Lightweight 8 byte hash function algorithm

Related

Given a GUID, if i shorten it .. can we assume it will still be unique?

Given that GUID's are (more or less) unique, if we shorten it with some code which:
It basically just converts a GUID into a base64 string and shortens it a bit.
It takes a standard GUID like this:
c9a646d3-9c61-4cb7-bfcd-ee2522c8f633
And converts it into this smaller string:
00amyWGct0y_ze4lIsj2Mw
Can I now assume that the shortened guid is equally as unique as it's previous (normal) form?
This is a reversable transform — you can get the original GUID back with an inverse function. That means that it's exactly as "unique"; there's a different "shortened GUID" for every GUID. The final substr step in the encode function is removing the base64 padding characters ==. This doesn't lose any information because every GUID is the same length and therefore every GUID has the same padding. The decode function re-appends "==" before passing to the base64 decoder.

errors, incorrect values when comparing sha512 as3, with vb.net

ive been testing an sha512 class. i need to generate a hash from a string within flash cs5, but i need it to match the hash produced by asp.net(vb). it appears to be adding a zero somewhere in the string, and i dont know why.
these are the files im using: Porting SHA512 Javascript implemention to Actionscript.
the hashed string is the name "Karla" in this example
example (asp.net)// ** the brackets show where the difference is ** C4DB628AD520AFF7308ED19E91635E8E24A6C7CFD4DB2F71BBE2FA6CD63770B315A839143037BB9DB16784C0BDCEB622ECAA4077D4D8(1787)D5023E86734748
(as3)
C4DB628AD520AFF7308ED19E91635E8E24A6C7CFD4DB2F71BBE2FA6CD63770B315A839143037BB9DB16784C0BDCEB622ECAA4077D4D8(17087)D5023E86734748
there's added info below, in the link i provided, but i do not think it related to what i need, i dont think im using hmac, just a straight string hash, however, when i do it in vb.net i get the bytes from the string first the i has the bytes.
I had a feeling that the as3 code converted the string automatically in the sha512 class?
hoping someone came across this issue as well.
thanks for any help with this.
Neither one of those hashes are correct. The correct SHA512 hash for the string "Karla" is:
C4DB628AD520AFF7308ED19E91635E8E24A6C7CFD4DB2F71BBE2FA6CD63770B315A839143037BB9DB16784C0BDCEB622ECAA4077D4D817087D5023E867347408
However, I would wager that the AS3 hash is actually correct -- the javascript version generates the correct hash, see here -- and was just pasted incorrectly.
In two places in the computed hash, it contains the byte 0x08, but in the ASP.NET version high 4 bits of the byte are being lost, and its being appended to the output string as just "8" not "08".
Basically, your ASP.NET hash generator is trashing numbers less than 0x10 -- ignoring the leading zero -- and giving you malformed hashes..
Another way to tell that there is something amiss with your ASP.NET hash is that its only 126 characters (504 hex encoded bits) long.

How do I store the Twitter id in MySQL?

The Twitter seach API returns a JSON object, a.o. containing the unique id of a tweet:
[id] => 2.9851634017882E+17
[text] => foobar
I have created a MySQL bigint(20) field in my database to store the id's. However, when I try to store the id, in the database it only says 2 in (in this instance).
What am I doing wrong?
For every id in the call, I would recommend an approach of converting the value to a string before sending it over to PHP.
I'm doing this with my own webapp and it works great. I faced the exact same issue with the API.
ex. using jquery with the Twitter API
$.getJSON('https://api.twitter.com/1.1/search/tweets.json?q=api&callback=?', function(data){
$.each(data, function() {
this.id_str = this.id_str + ""; // cast integer as string to avoid PHP int to float conversion on json_decode
/* ... more API parsing code ... */
also make sure your capturing the id_str value and not the id value. id_str is perferred over id and is less error prone when dealing with larger id's.
Twitter IDs are classified as 64-bit integers. Storing them as such is the best course of action. In MySQL the datatype for this is BIGINT.
Because different JSON interpreters struggle with parsing BIGINT my preferred approach is to consume the feed according to their specs:
...consider converting to the _str versions of IDs as soon as
possible.
As a JSON consumer, I would interpret the _str version of the id and insert this "safe" value into your BIGINT column.
MySQL doesn't understand 2.9851634017882E+17 as an INT since it isn't. It truncates the number to the longest integer it finds from the beginning of the input, which is 2. You need to put it in the query as an int, not in scientific notation. Also, if that's exactly what you're getting back from Twitter, you don't even know the ID since the precision of that number doesn't go down to the ones place.
The ID you get is a float. I would be highly surprised if twitter gives you floats as ID's.
Are you sure that the JSON object is not parsed by something that converts all integers to floats? When you fixed that then problem in saving the value will be solved.

Find out the type of a Profobuf message (Google Chrome Sync)

I'm trying to connect to Google Chrome sync (that synchronizes your Chrome settings and your currently opened tabs).
For now I'm concentrating on on the tab syncing. I connected to the Google Talk servers and I'm receiving messages from tango bot whenever I navigate to a new webpage in Chrome.
But I have difficulties decoding those messages as they are encodes in Google's protobuf format – because there are tons of different protobuf classes dedicated to Chrome Sync and I think there's no way of figuring out the type of a binary protobuf message?
A typical message would look like this (base64 encoded, XXXX't out my mail adress):
CAAilQEKQAoGCgQIAxACEiUKBgoECAMQARISCZwF6dZYmkeFEXZLABNN3/yMGgcIhSwQAxgBINP80ri/JyoIMTgxOTgxMjYaUQpPCgwI7AcSB1NFU1NJT04QARiw64/I0se0AiIyVzpDaGZDeU9JWUZXdXFuUmRXaGtJWk94VkRSM1lmTGU1M0FoRGVxT2EwOHVQUHcyOD0wASoGCgQIAxACMAI4AUIrCG8SJxAEGAIiFGRlbHXXXXXXXXdAZ21haWwuY29tQgl0YW5nb19yYXdIAQ==
I tried decoding it with some of the protobuf classes (that I compiled for Java), but with none of them I got any useful data.
Does anyone have more information on this topic? Some insight on how to find the right protobuf class for decoding a certain binary message would be great. It would even help me to some point to be able to decode that exact message I gave as an example above.
There is very little public documentation and the Chromium source code is really difficult to look trough if you're not a C++ guy…
(I'm developing in Java, if that matters)
Yes, that is broadly possible; however, it cannot be done with the data you have posted because you have corrupted it irretrievably in your attempt to remove your email address. Protobuf is very sensitive to that; I tried replacing the XXXXXXXX with the base-64 for a 6-letter email-address, but the byte immediate before that is 199, and 199 cannot be legal there (the data immediately before string contents is the length of the string encoded as a varint, and a varint can never end with the most-significant-bit of the last byte set, because the MSB is a continuation flag).
If you have raw protobuf binary, you can try running it through protoc --decode_raw, and see what it says; that may give you enough to start reconstructing the layout. Alternatively, you can try parsing it manually with your preferred implementation's "reader" API (if it has one). For example, using protobuf-net and ProtoReader, I was able to piece together (the numbers in brackets are the offsets after reading each field-header):
{
(1) field 1: varint, value 0 if int
(3) field 4: string, looks like sub-message
// everything after this point is really really suspect
(6) field 1, string, looks like sub-message
(8) field 1, string, looks like sub-message
(16) field 2, string, looks like sub-message
(55) field 4, varint, 1357060030035 assuming int64
(62) field 5, string; "18198126"
(72) field 3, string, looks like sub-message
(64) field 1, string, looks like some encoded session data
(155) field 5: string, looks like sub-message
(157) field 1: string, looks like sub-message
(163) field 6: varint, value 2 if int
(165) field 7: varint, value 1 if int
(167) field 8: string, looks like sub-message
(169) field 1: varint, value 111 if int
(171) field 2: string, looks like sub-message
}
The problem is that due to the corruption (because of your replacement), it is impossible to say much beyond that field 4; by that point, everything could be completely gibberish due to the lengths being off. So I have very little confidence past that point. The main point of the above is simply to illustrate: yes, you can parse protobuf data without knowing the schema in advance, to reverse engineer a schema - but it requires:
patience and a little guesswork to interpret each field (each wire-type can mean multiple things)
if you know what the values being stored, without necessarily knowing how each maps to fields, then you have a headstart; for example, if you know you are being sent something with the values 22, 1325, "hello world", and 123.45F; then you should be able to figure out the mapping easily enough
intact data (which is sadly missing in this case)

How can I populate a query string variable to a text box which contains &,\ and $ in it

I have a variable like say A= drug & medicare $12/$15.
I need to assign it to a text box, but only 'drug' is posted the server. The rest of the data gets truncated.
this.textbox.text= request.querystring["A"].tostring();
The following is not valid for a="foo&bar$12":
http://example.com?a=foo&bar$12
The & symbol is a reserved character, it seperates query string variables. You will need to percent encode a value before sending them to that page.
Also & is a reserved character in HTML/XML. I suggest reading up on percent encoding and html encoding.
I believe you have problems with HTML entities. You need to read up on HTML escaping in your tool of choice. & cannot stand in HTML, since it begins an entity sequence - it needs to be replaced with &. Without specifying at least which toolchain you're using (as per #Richard's comment), we can't really suggest the best way to do it.
EDIT: Now that I reread your question, it seems A is not a variable but a query parameter :) Reading comprehension fail. Anyway, in this case a similar problem exists: & is not a valid character for a query parameter, and it needs URL escaping. Again, how exactly to do it is in the documentation for your toolchain, but in essence & will need to be replaced by %26. Plus sign is also not permitted (or rather it has another meaning); others are tolerated (but there are nicer ways to write them).
That looks more or less like ASP.NET pseudocode, so I'm going to diagnose your problem as the query string needing to be URL encoded. Key/value pairs in the query string are separated by an ampersand (&), and ASP.NET (along with other web platforms) automatically parse out the key value pairs for you.
In this case, the ampersand terminates the value of the "A=..." key/value pair. The problem will be solved if you can URL encode the link that brings the user into your page. If actually using ASP.NET, you can use the HttpUtility.UrlEncode() method for that:
string myValue = Server.UrlEncode("drug & medicare $12/$15");
You'll end up with this querystring instead: A=drug%20%26%20medicare%20%2412%2F%2415