I need to generate the ODIN-1 of a string in Python. The official documentation specifies applying SHA-1 to the input string/identifier, but I'm not sure if I need to perform other operations to it beforehand? Also, is the final output the hex digest of the SHA-1 or something else?
E.g. How can I convert this MAC to ODIN-1 in Python? "74e2f543d2ce"
Thanks in advance!
from hashlib import sha1
def odin1(mac_addr):
"""SHA1 hexdigest of hex representaiton of MAC address"""
to_hash ''.join([i.decode('hex') for i in mac_addr.split(':')])
return sha1(to_hash).hexdigest()
>>> odin1('1a:2b:3c:4d:5e:6f')
'82a53f1222f8781a5063a773231d4a7ee41bdd6f'
Let's break this down, line by line between the documentation you linked to, and my answer:
// NOTE: iOS returns MAC Address NOT a string, but a 6-byte array.
// A human readable MAC Address may be represented as the following:
#"1a:2b:3c:4d:5e:6f";
#"1A2B3C4D5E6F";
In python:
>>> '1A'.decode('hex') == '1a'.decode('hex')
True
So we can convert the string given to us into a more agreeable format (that reduces "any ambiguity around punctuation and capitalization"):
>>> mac = "1a:2b:3c:4d:5e:6f".split(':')
>>> hex_mac = [m.decode('hex') for m in mac]
>>> hex_mac
['\x1a', '+', '<', 'M', '^', 'o']
We can treat this list as a string (just the same as if we used a byte array) to get the same result from the SHA1 hash function.
Of course, we can receive MAC addresses this way:
>>> mac = '1A2B3C4D5E6F'
>>> hex_chunks = lambda s: [s[i: i+2] for i in range(0, len(s), 2)]
>>> [m.decode('hex') for m in hex_chunks(mac)]
['\x1a', '+', '<', 'M', '^', 'o']
So it would be up to us to properly unify the input for the single function to operate across all possible forms. Regardless, our function could take either form, the end result is what matters:
>>> sha1(''.join(['\x1a', '+', '<', 'M', '^', 'o'])).hexdigest()
Will produce the correct hash (according to the link you posted).
Hope this helps make my answer clearer.
I need to generate the ODIN-1 of a string in Python.
No you don't, not according to the docs.
You generate an ODIN-1 of an 802.11 MAC address, ANDROID_ID, or DeviceUniqueID. Some relevant quotes:
The seed should be left unaltered from the format returned by the operating system.
NOTE: iOS returns MAC Address NOT a string, but a 6-byte array" right underneath the chart.
… representing it as a raw byte array prevents any ambiguity around punctuation and capitalization:
And IIRC, ANDROID_ID is a 64-bit integer, neither a MAC nor a string. (I don't know about DeviceUniqueId on Windows Phone.)
So, you probably need to generate the ODIN-1 of a 6-byte array [0x74, 0xe2, 0xf5, 0x43, 0xd2, 0xce], not a 12-character string "74e2f543d2ce". The sample shows how to do that in Objective-C; in Python, it's:
mac = bytes([0x74, 0xe2, 0xf5, 0x43, 0xd2, 0xce])
Or, since your question specifies Android, presumably you don't want the MAC address at all, in any format… but I'll assume that was just a mistaken tag, and you're using iOS, and do want the MAC address.
How do you do that?
Hash Step: Pass the Identifier Seed through the SHA-1 hash function.
In Python, that's:
hash = hashlib.sha1(mac)
The resulting message digest is ODIN-1.
In Python, that's:
digest = hash.hexdigest()
Putting it together:
hashlib.sha1(bytes([0x74, 0xe2, 0xf5, 0x43, 0xd2, 0xce])).hexdigest()
The result is a "40 lowercase character string", just as the docs say it should be:
'10f4ab0775380aceaca5a2733604efa6d6364b08'
Also, if you're looking for clarification on a preliminary spec posted on a wiki page, why would you ask about it at SO instead of posting a comment on that page?
To answer your first specific question:
I'm not sure if I need to perform other operations to it beforehand?
The spec says:
The seed should be left unaltered from the format returned by the operating system.
To answer your second:
Also, is the final output the hex digest of the SHA-1 or something else?
The spec says:
The resulting message digest is ODIN-1.
// The format of this hash should be a 40 lowercase character string:
Meanwhile, there's sample code attached to the project (as you'd expect, given that it's at googlecode)… but it's not that helpful.
The iOS sample is completely missing the relevant code. It's a generic GUI app generated by the wizard, with an added #import "ODIN.h" and textView.text = [ODIN1() lowercaseString]; in the viewDidLoad. But that ODIN.h file, and the corresponding ODIN.m or libODIN.a or whatever doesn't appear to be anywhere. (From a brief glance at the project.pbxproj, there's clearly supposed to be more files, which they apparently just didn't check in.)
The Android sample does have the relevant code, but it clearly violates the spec. It gets the ANDROID_ID as a Unicode string, then encodes it to iso-8859-1, calls SHA-1 on the resulting bytes, and generates a hex digest out of it. The docs explicitly say to use the OS value exactly as returned by the OS; the code Latin-1 encodes it instead.
The Windows sample, on the other hand, does seem to do what the docs say—it gets the DeviceUniqueId as a byte[], and uses it as-is. (However, the code won't actually work, because it's using an obsoleted API call, which throws an exception rather than return a byte[]…)
At this point, I have to ask why you're following this spec in the first place. If you're trying to interoperate with someone else's code, you probably care which of the contradictory ways of interpreting this spec is being used by that code, rather than trying to guess which one the designers intended.
Not to mention that Apple has explicitly told people not to use anything based on the MAC to replace the UDID, and ODIN is something trivially based on the MAC to replace the UDID…
Related
I need a regex for android signature hash which is used in azure in a field presented on picture.
I used something like that:
"^(?=.{28}$)(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$"
It's matching words base64-encoded with 28 characters words but it will also match e.g. platformAndroidSignatureHash which azure decline (presented on a picture).
WORKING EXAMPLE:
2pmj9i4rSx0yEb/viWBYkE/ZQrk=
aPz8/NARbPz8pPzg/Iz9aPz8NCg=
It matches platformAndroidSignatureHash because the last part with the equals signs is optional.
You could rewrite the pattern as
^(?=.{28}$)(?:[A-Za-z0-9+/]{4})+[A-Za-z0-9+/]{2,3}==?$
The pattern matches;
^ Start of string
(?=.{28}$) Positive lookahead, assert 28 characters
(?:[A-Za-z0-9+/]{4})+ Repeat 1+ times 4 chars of the listed in the character class
[A-Za-z0-9+/]{2,3} Repeat 2-3 times matching one of the listed chars
==? Match either = or ==
$ End of string
Regex demo
get familiar with base64 encoding, especially read about padding, but in short: your "faked" String should probably ends with = or ==. you can't use regexp in here, as you should do some bitwise math, as every base64 digit needs 6 bits, in the meanwhile common usage (e.g. printing on screen) would use 8 bits per digit. you have to calculate/respect this padding. more info in THIS topic (read ALL answers and comments, accepted one isn't reliable!)
I'm working on a solution where need to encode string into utf-8 format, this string nothing but device name that I'm reading using BluetoothAdapter.getDefaultAdapter().name.
For one of sampple I got a string like ABC-& and encoding this returned ABC-%EF%BC%86 instead of ABC-%26. It was weird until further debugging which helped to identify that there is difference between & and &. Second one is some other character which is failing to encoded as expected.
& and & both are different.
For encoding tried both URLEncoder.encode(input, "utf-8") and Uri.encode(input, "utf-8") but nothing worked.
This is just an example, there might be other character which may look like same as actual character but failed to encode. Now question are:
Why this difference, after all it is reading of some data from device using standard SDK API.
How can fix this be fixed. Find and replace with actual character could be a approach but scope is limited, there might be other unknown character.
Any suggestion around !!
One solution would be to define your allowed character scope. Then either replace or remove the characters that fall outside of this scope.
Given the following regex:
[a-zA-Z0-9 -+&#]
You could then either do:
input.replaceAll("[a-zA-Z0-9 -+&#]", "_");
...or if you don't care about possibly empty results:
input.replaceAll("[a-zA-Z0-9 -+&#]", "");
The first approach would give you a length-consistent representation of the original Bluetooth device name.
Either way, this approach has worked wonders for me and my colleagues. Hope this could be of any help 😊.
I have been trying to download a file from Amazon S3 that ends with special character.
The file name ends with an "=" as a result of Base64 encoding. Now I am trying to download this file and I receive an error,
The specified key does not exist. (Service: Amazon S3; Status Code: 404; Error Code: NoSuchKey;
I tried URL Encoding the string. So now the "=" becomes "%3D" and still I receive the same error.
But if I remove the "=" from the file name, I am being able to download the file without issues. But this is a common file and it is to be accessed form iOS as well.
NOTE: The iOS Amazon SDK works even when the file name has "=" in it.
The issue is faced only in Android SDK.
According to AWS documentation
Safe Characters
The following character sets are generally safe for use in key names:
Alphanumeric characters [0-9a-zA-Z]
Special characters !, -, _, ., *, ', (, and )
and
Characters That Might Require Special Handling
The following characters in a key name may require additional code handling and will likely need to be URL encoded or referenced as HEX. Some of these are non-printable characters and your browser may not handle them, which will also require special handling:
Ampersand ("&")
Dollar ("$")
ASCII character ranges 00–1F hex (0–31 decimal) and 7F (127 decimal.)
'At' symbol ("#")
Equals ("=")
Semicolon (";")
Colon (":")
Plus ("+")
Space – Significant sequences of spaces may be lost in some uses (especially multiple spaces)
Comma (",")
Question mark ("?")
So it confirms you that "=" require special handling,
It will be better if you replace the last "=" char with another safe char to avoid the issue ...
Please try to change the "=" to "="
As on iOS, there is no issue, I expect that it could be relative to the Android environment.
You may note that some chars could also be forbidden because the SH or BASH or ANDROID shell environment execution,
please also to take in consideration that some disk format option (FAT32 on a normal android external memory card) could also represent a factor which forbids some char in the filename.
If you take a look here and more especially on the #kreker answer:
According to wiki and assuming that you are using external data storage which has FAT32.
Allowable characters in directory entries
are
Any byte except for values 0-31, 127 (DEL) and: " * / : < > ? \ | + , . ; = [] (lowcase a-z are stored as A-Z). With VFAT LFN any Unicode except NUL
You will note that = is not an allowed char on the android FAT32 partition ...
As I expect that Android will consider = as restricted char you may try to escape it with a \= or add a quote to the file name on your code ...
An example with a copy:
cp filename=.png mynewfile=.png #before
cp "filename=.png" "mynewfile=.png" #after
"VCS...=.png"
If nothing of this tricks will work you have to change the filename to remove the "=" when you create those files.
Regards
The following characters in a key name may requiere additional code
handling and will likely need to be URL encoded or referenced as
HEX.
Some of these are non-printable characters and your browser may not handle them, which will also require special handling:
The best practices to ensure compatibility between applications defining Key names are using:
- Alphanumeric characters [0-9a-zA-Z]
- Special characters !, -, _, ., *, ', (, and )
Using android you need to encode the file name, the character (commonly used as operator):
=
to :
%3D
First of all I think you are using CopyObjects method of s3. OR you received a file name from an s3 event or somewhere else which you are trying to download. The issue is aws handles special characters differently when they store the names. If you'll go to s3 console and click on the file name. You'll see the URI which will have different values for special characters like space will be replaced by + like that. So you need to handle the special characters accordingly. Misleading examples wont help you as aws has constraints on file names but if you save them otherwise it will replace them with acceptable characters and your actual file name will be different than the one you uploaded hence you getting file not found
Hmm, I can't find the man page for 'replace' in Googles App scripts, I only see 'replaceText'. Anyway, from what I gather from the SO posts, the below should work, hopefully someone can spot it easily.
The String in the Cell is "[pro] all, everybody" and I want to remove the bracketed word '[pro]' so the result is 'all, everybody'.
It does work just fine with:
Cell = Cell.toString().replace("\[pro\]","");
but when I try to make it generic, it fails with all these (not sure what the pattern matching rules are, thus the question for the man page):
Cell = Cell.toString().replace("\[pr.\]","");
Cell = Cell.toString().replace("\[pr.*\]","");
Cell = Cell.toString().replace("\[.*\]","");
they should work, no ? What am I missing ?
Also, how would I use 'replaceText', I can't seem to apply it directly to the 'Cell' object.
The String#replace is a JavaScript function where you need to use a regex with a regex literal notation or with new RegExp("pattern", "modifiers") constructor notation:
Cell = Cell.toString().replace(/\[pr[^\]]*]/,"");
When using a regex literal, backslashes are treated as literal backslashes, and /\d/ matches a digit. The constructor notation equivalent is new RegExp("\\d").
The /\[pr[^\]]*]/ regex matches the first instance of:
\[pr - literal substring [pr
[^\]]* - 0+ chars other than ]
] - a literal ] symbol.
And replaces with an empty string.
I am trying to parse a Rss2.0 feed on Android using a Pull parser.
XmlPullParser parser = Xml.newPullParser();
parser.setInput(url.open(), null);
The prolog of the feed XML says the encoding is "utf-8". When I open the remote stream and pass this to my Pull Parser, I get invalid token, document not well formed exceptions.
When I save the XML file and open it in the browser(FireFox) the browser reports presence of Unicode 0x12 character(grave accent?) in the file and fails to render the XML.
What is the best way to handle such cases assuming that I do not have any control over the XML being returned?
Thanks.
Where did you find that 0x12 is the grave accent? UTF-8 has the character range 0x00-0x7F encoded the same as ASCII, and ASCII code point 0x12 is a control character, DC2, or CTRL+R.
It sounds like an encoding problem of some sort. The simplest way to resolve that is to look at the file you've saved in a hex editor. There are some things to check:
the byte order mark (BOM) at the beginning might confuse some XML parsers
even though the XML declaration says the encoding is in UTF-8, it may not actually have that encoding, and the file will be decoded incorrectly.
not all unicode characters are legal in XML, which is why firefox refuses to render it. In particular, the XML spec says that that 0x9, 0xA and 0xD are the only valid characters less than 0x20, so 0x12 will definitely cause compliant parsers to grumble.
If you can upload the file to pastebin or similar, I can help find the cause and suggest a resolution.
EDIT: Ok, you can't upload. That's understandable.
The XML you're getting is corrupted somehow, and the ideal course of action is to contact the party responsible for producing it, to see if the problem can be resolved.
One thing to check before doing that though - are you sure you are getting the data undisturbed? Some forms of communication (SMS) allow only 7-bit characters. This would turn 0x92 (ASCII forward tick/apostrophe - grave accent?) into 0x12. Seems like quite a coincidence, particularly if these appear in the file where you would expect an accent.
Otherwise, you will have to try to make best do with what you have:
although not strictly necessary, be defensive and pass "UTF-8" as the second paramter to setInput, on the parser.
similarly, force the parser to use another character encoding by passing a different encoding as the second parameter. Encodings to try in addtion to "UTF-8" are "iso-8859-1" and "UTF-16". A full list of supported encodings for java is given on the Sun site - you could try all of these. (I couldn't find a definitive list of supported encodings for Android.)
As a last resort, you can strip out invalid characters, e.g. remove all characters below 0x20 that are not whitespace (0x9,0xA and 0xD are all whitepsace.) If removing them is difficult, you can replace them instead.
For example
class ReplacingInputStream extends FilterInputStream
{
public int read() throws IOException
{
int read = super.read();
if (read!=-1 && read<0x20 && !(read==0x9 || read==0xA || read==0xB))
read = 0x20;
return read;
}
}
You wrap this around your existing input stream, and it filters out the invalid characters. Note that you could easily do more damage to the XML, or end up with nonsense XML, but equally it may allow you to get out the data you need or to more easily see where the problems lie.
I use to filter it with a regex, but the trick is not trying to get and replace the accents. It depends on the encode and you don't want to change the content.
Try to insert the content of the tags into this tags
Like this
<title>My title</title>
<link>http://mylink.com</link>
<description>My description</description>
To this
<title><![CDATA[My title]]></title>
<link><![CDATA[http://milynk.com]]></link>
<description><![CDATA[My Description]]></description>
The regex shouldn't be very hard to figure out. It works for me, hope it helps for you.
The problem with UTF-8 is that it is a multibyte encoding. As such it needs a way to indicate when a character is formed by more than one byte (maybe two, three, four, ...). The way of doing this is by reserving some byte values to signal multibyte characters. Thus encoding follows some basic rules:
One byte characters have no MSB set (codes compatible with 7-bit ASCII).
Two byte characters are represented by sequence: 110xxxxx 10xxxxxx
Three bytes: 1110xxxx 10xxxxxx 10xxxxxx
Four bytes: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
Your problem is that you may be reading some character string supposedly encoded as UTF-8 (as the XML encoding definition states) but the byte chunk might not be really encoded in UTF-8 (it is a common mistake to declare something as UTF-8 but encoding text with a different encoding such as Cp1252). Your XML parser tries to interpret byte chunks as UTF-8 characters but finds something that does not fit the encoding rules (illegal character). I.e. two bytes with two most significate bytes set would bring an illegal encoding error: 110xxxxx must be always followed by 10xxxxxx (values such as 01xxxxxx 11xxxxxx 00xxxxxx would be illegal).
This problem does not arise when non-variable length encodings are used. I.e. if you state in your XML declaration that your file uses Windows-1252 encoding but you end up using ANSI your only problem will be that non-ASCII characters (values > 127) will render incorrectly.
The solution:
Try to detect encoding by other means.
If you will always be reading data from same source you could sample some files and use an advanced text editor that tries to infer actual encoding of the file (i.e. notepad++, jEdit, etc.).
Do it programatically. Preprocess raw bytes before doing any actual xml processing.
Force actual encoding at the XML processor
Alternatively if you do not mind about non-ASCII characters (no matter if strange symbols appear now and then) you could go directly to step 2 and force XML processing to any ASCII compatible 8-byte fixed length encoding (ANSI, any Windows-XXXX codepage, Mac-Roman encoding, etc.). With your present code you just could try:
XmlPullParser parser = Xml.newPullParser();
parser.setInput(url.open(), "ISO-8859-1");
Calling setInput(istream, null) already means for the pull parser to try to detect the encoding on its own. It obviously fails, due to the fact that there is an actual problem with the file. So it's not like your code is wrong - you can't be expected to be able to parse all incorrect documents, whether ill-formed or with wrong encodings.
If however it's mandatory that you try to parse this particular document, what you can do is modify your parsing code so it's in a function that takes the encoding as a parameter and is wrapped in a try/catch block. The first time through, do not specify an encoding, and if you get an encoding error, relaunch it with ISO-8859-1. If it's mandatory to have it succeed, repeat for other encodings, otherwise call it quits after two.
Before parsing your XML, you may tweak it, and manually remove the accents before you parse it.
Maybe not the best solution so far, but it will do the job.