Objective C - Equivalent of PHP Pack - ios

What's the exact equivalent of the PHP function pack in Objective-C ?
For example :
$key = pack('H*', 'ABEFFF' );
$result = hash_hmac("SHA512", "Awesome message", $key)
I am looking for the exact same string that the one returned by the PHP pack function.
I already read this subject : https://stackoverflow.com/a/8950667/2199320
But i can't get to have the correct NSString from the NSData (ASCII, UTF-8, base 64...).
Or if this function really works, i need a way to have a hmac hash for a NSData as a the salt and a NSString as the message...
I tried a lot of differents solutions but none of them were working...

Related

How do I convert esp.flash_read() to string

I have the following code:
buff=esp.flash_read(esp.flash_user_start(),50)
print(buff)
I get the following output from print:
bytearray(b'{"ssid": "mySSID", "password": "myPASSWD"}\xff\xff\xff\xff\xff\xff')
What I want to do is get the json in buff. What is the correct "Python-way" to do that?
buff is a Python bytes object, as shown by the print output beginning with b'. To convert this into a string you need to decode it.
In standard Python you could use
buff.decode(errors='ignore')
Note that without specifying errors=ignore you would get a UnicodeDecodeError because the \xff bytes aren't valid in the default encoding, which is UTF-8; presumably they're padding and you want to ignore them.
If that works on the ESP8266, great! However this from the MicroPython docs suggests the keyword syntax might not be implemented - I don't have an ESP8266 to test it. If not then you may need to remove the padding characters yourself:
textLength = find(buff, b'\xff')
text = buff[0:textLength].decode()
or simply:
text = buff[0:buff.find(b'\xff')].decode()
If decode isn't implemented either, which it isn't in the online MicroPython interpreter, you can use str:
text = str(buff[0:find(buff, b'\xff')], 'utf-8')
Here you have to specify explicitly that you're decoding from UTF-8 (or whatever encoding you specify).
However if what you're really after is the values encoded in the JSON, you should be able to use the json module to retrieve them into a dict:
import json
j = json.loads(buff[0:buff.find(b'\xff')])
ssid = j['ssid']
password = j['password']

RestKit path pattern for comma separated dynamic arguments

I am using RestKit, i have send a single GET request to get a bulk data to a URL like this
api/exemptions?ids=203,1985,21855
What path pattern can be set for this in RestKit response descriptor?
I know for predefined number of dynamic argument we can use something like this #"/api/sms/confirmation/:arg1/:arg2"
but above mentioned case is new for me.
EDIT
I found that parameter argument in
[[RKObjectManager sharedManager] getObjectsAtPath:path parameters:nil
will do the job, but it requires a dictionary so i am giving it an example dictionary NSDictionary *args = #{ #"ids" : #[#"1",#"2",#"3",#"4"] };
when executed this encoded url is generated
http://../api/exemptions?&ids%5B%5D=1&ids%5B%5D=2&ids%5B%5D=3&ids%5B%5D=4
"ids" key is repeating, what is going wrong here.
EDIT # 2
URL encoding problem is solved, but the main problem still persists, path pattern is not matching on response, I am using this path pattern currently
pathPattern:#"/api/exemptions?&ids"
for this url /api/exemptions?ids=203,1985,21855
i have also tried pathPattern:#"/api/exemptions?&ids="
Please help, This is becoming huge pain.
Based on your sample code and response, have you tried:
NSDictionary *args = #{ #"ids": [#[#"1", #"2"] componentsJoinedByString: #","] };
This looks like it would encode with the desired value, since the joining leads to a dictionary value of #{ #"ids": #"1,2" }.

Joomla database array to json conversion

I am trying to convert the data that I selected from mysql database to json format. I am using Joomla 3.2.1 so that I can use it for my iOS application.
I am getting syntax error unexpected Jresponse t_string error near JResponse.
I would appreciate if anyone can point me in the right direction.
thank you.
<?php
defined ('_JEXEC') or die('');
require_once JPATH_SITE.'/components/com_content/helpers/route.php';
jimport('joomla.application.component.controller');
jimport('joomla.appliction.component.model');
$db = JFactory::getDbo();
// Create a new query object.
$query = $db->getQuery(true);
$query->select($db->quoteName(array('order_id', 'store_name', 'invoice', 'firstname')));
$query->from($db->quoteName('#__mynicetable'));
$query->where($db->quoteName('order_id') );
$query->order('order_id ASC');
$db->setQuery($query);
$row=$db->loadRowList();
print_r($row);
$data =array ($row);
$document= JFactory::getDocument;
$document-> setMimetEncoding('application/json')
JResponse::setHeader('Content-Disposition', 'attachment;filename="'.$view- >getName().'.json"');
echo json_encode($data);
You have some gaps in your code and a missing semi-colon. Try using the following:
$data = array($row);
$app = JFactory::getApplication();
$document = JFactory::getDocument();
$document->setMimetEncoding('application/json');
$app->setHeader('Content-Disposition', 'attachment;filename="my-scratchcomponent.json"');
echo json_encode($data);

PHP: newbie question - parse xml from string?

i've googled and found several php extensions for php so i was wondering:
is there already a built-in support for parsing xml in php?
i'd like to convert my projects from asp to php - i got xml data stored in a database - so i need an xml parser for php which can accept xmldata as string (not load-from-file) - any ideas how to do it?
thanks
SimpleXML
To be precise: simplexml_load_string — Interprets a string of XML into an object
object simplexml_load_string ( string $data [, string $class_name = "SimpleXMLElement" [, int $options = 0 [, string $ns [, bool $is_prefix = false ]]]] )
Example:
$string = // your XML string
$xml = simplexml_load_string($string);

How do I convert a Bit Torrent info_hash (obtained from Wireshark) to a SHA1 hash

I'm running Snort which detects some P2P activity, specifically the BitTorrent announce request. I see the HTTP GET /announce.php?info_hash=XXX... request and I'm trying to convert this XXX into a proper SHA1 hash to try and get an idea of what is being downloaded.
I've read various things that say this is URL encoded, and others that say just remove the % character - however I am unable to reproduce this.
Can anyone suggest how to do this?
info_hash is an SHA1 hash. It's a binary hash, URL-encoded for inclusion in a URL.
If you want to turn it into a hex-encoded hash, you will need to extract it from the URL, URL-decode, and hex-encode. For example in Python:
>>> '%00%01%02%20%25ABC+XYZabc%7F%80%81%FE%FF'
'%00%01%02%20%25ABC+XYZabc%7F%80%81%FE%FF'
>>> urllib.unquote_plus(_)
'\x00\x01\x02 %ABC XYZabc\x7f\x80\x81\xfe\xff'
>>> _.encode('hex')
'00010220254142432058595a6162637f8081feff'
Okay, know I know. info_hash is an SHA1 hash. And an example of it is: %5d%97%dbA%d7a%2b%92%f5%c2%ef%dcv%bf%e7%e6%03%24%85%0a. If you use $_GET['info_hash'], it will not work, because of the %s.
You need to use $_SERVER['QUERY_STRING'].
Code example how to get SHA1 hash of info_hash in PHP:
$arye = $_SERVER['QUERY_STRING'];
$arye = explode('info_hash=', $arye)[1];
$arye = explode('&', $arye)[0];
$arye = explode('%', $arye);
$arp = '';
foreach($arye as $ara) {
if (strlen($ara) == 2) {
$arp .= $ara;
}else{
$e1 = substr($ara, 0, 2);
$e2 = substr($ara, 2, 1);
$e2 = unpack('H*', $e2)[1];
$arp .= $e1;
$arp .= $e2;
}
}
echo $arp; // This will be your SHA1 hash
Hash: %5d%97%dbA%d7a%2b%92%f5%c2%ef%dcv%bf%e7%e6%03%24%85%0a -> 5d97db41d7612b92f5c2efdc76bfe7e60324850a
No, you can,
infohash == sha1(torrentfile["info"])
But you could use info_hash as key to search it on DHT network

Resources