Difficulty translating Python3 get hotp/totp token code to Swift3 - python

Recently I found this awesome 2-factor authentication code generator written in Python 3. I was trying to convert it to Swift 3, but I am having trouble with one specific part, though:
def get_hotp_token(secret, intervals_no):
key = base64.b32decode(secret)
msg = struct.pack(">Q", intervals_no)
h = hmac.new(key, msg, hashlib.sha1).digest()
o = h[19] & 15
h = (struct.unpack(">I", h[o:o+4])[0] & 0x7fffffff) % 1000000
return h
I so far have only been able to do the first line of the function body :p using code from here
func getHotpToken(secret: String) -> [Int] {
let data = secret.base32DecodedData
<...>
return theTokens
}
I tried reading the documentation on struct.pack here and reading about what packing actually is here, but I still find the concept/implementation confusing, and I have no idea what the equivalent would be in Swift.
According to the documentation, struct.pack returns a string in the given format. The format in my case is >Q, which means that the byte order is little-endian and the C Type is an unsigned long long. Again, I am not exactly sure how this is supposed to look in Swift.
... And that is only the second line! I don't really understand how HMAC works (I can't even find the actual 'background' code), so I can't even translate the entire function. I could not find any native library for Swift that has this behavior.
Any pointers or help translating this function will be highly appreciated!
P.S. I checked and I think that this is on topic
Relevant imports:
import base64, struct, hmac

I just finished converting my code to Swift 3. This is a little different from the Python version, since this is more of a framework-type thing. It took a lot of experimentation to get the get_hotp_token to work (for example the Wikipedia page says it used SHA256 but it actually uses SHA1.
You can find it here.
When you use this, be sure to add a bridging header with #import <CommonCrypto/CommonHMAC.h>
Enjoy!

Related

Can anyone shed some light on why this code from the alpaca-py documentation does not work?

I am trying to stream bitcoin data using the alpaca-py trading documentation but I keey getting a invalid syntax error. This is taken exactly from the alpaca-py documentation. Does anyone know what I am doing wrong?
from typing import Any
from alpaca.data.live import CryptoDataStream
wss_client = CryptoDataStream(key-id, secret-key)
# async handler
async def quote_data_handler(data: Any):
# quote data will arrive here
print(data)
wss_client.subscribe_quotes(quote_data_handler, "BTC")
wss_client.run()
Take a look at the dashes in your parameters. Usually a no-no in most languages since the "-" or dash usually refers to a minus which is a binary operator or an operator that operates on two operands to produce a new value or result."
Make sure parameters are set before passing them.
Try the underscore instead as in: key_id = "".
Also useful is the following link to a comprehensive list of crypto pairs supported by Alpaca: https://alpaca.markets/support/alpaca-crypto-coin-pair-faq/#:~:text=For%20the%20initial%20launch%20of,%2C%20SOL%2C%20TRX%2C%20UNI)
Stay up to date on the above list as it's membership may be a bit volatile at the moment.

Convert two raw values to 32-bit IEEE floating point number

I am attempting to decode some data from a Shark 100 Power Meter via TCP modbus. I have successfully pulled down the registers that I need, and am left with two raw values from the registers like so:
[17138, 59381]
From the manual, I know that I need to convert these two numbers into a 32bit IEEE floating-point number. I also know from the manual that "The lower-addressed register is the
high order half (i.e., contains the exponent)." The first number in the list shown above is the lower-addressed register.
Using Python (any library will do if needed), how would I take these two values and make them into a 32 bit IEEE floating point value.
I have tried to use various online converters and calculators to figure out a non-programmatic way to do this, however, anything I have tried gets me a result that is way out of bounds (I am reading volts in this case so the end result should be around 120-122 from the supplied values above).
Update for Python 3.6+ (f-strings).
I am not sure why the fill in #B.Go's answer was only 2. Also, since the byte order was big-endian, I hardcoded it as such.
import struct
a = 17138
b = 59381
struct.unpack('>f', bytes.fromhex(f"{a:0>4x}" + f"{b:0>4x}"))[0]
Output: 121.45304107666016
The following code works:
import struct
a=17138
b=59381
struct.unpack('!f', bytes.fromhex('{0:02x}'.format(a) + '{0:02x}'.format(b)))
It gives
(121.45304107666016,)
Adapted from Convert hex to float and Integer to Hexadecimal Conversion in Python
I read in the comments, and #Sanju had posted this link: https://github.com/riptideio/pymodbus/blob/master/examples/common/modbus_payload.py
For anyone using pymodbus, the BinaryPayloadDecoder is useful as it's built in. It's very easy to pass a result.registers, as shown in the example. Also, it has a logging integrated, so you can help debug why a conversion isn't working (ex: wrong endianness).
As such, I made a working example for this question (using pymodbus==2.3.0):
from pymodbus.constants import Endian
from pymodbus.payload import BinaryPayloadDecoder
a = 17138
b = 59381
registers = [a, b]
decoder = BinaryPayloadDecoder.fromRegisters(registers, byteorder=Endian.Big)
decoder.decode_32bit_float() # type: float
Output: 121.45304107666016

How to Convert Markdown to JSON?

I need help with something.I need to convert the Markdown file to json format, but I don't know how to do this.I did a Google search, "markdown to json" but the tools I used didn't work for me.Is there someone who has experienced this before?
PS: I can use Nodejs and python for this.But I used the nodejs and python modules related to this did not work.
example Markdown
```{python}
from __future__ import division
from deltasigma import *
```
### 5th-order modulator: NTF *with* zeros optimization
This time we enable the zeros optimization, setting `opt=1` when calling synthesizeNTF(), then replot the NTF as above.
* 0 -> not optimized,
* 1 -> optimized,
* 2 -> optimized with at least one zero at band-center,
* 3 -> optimized zeros (with optimizer)
* 4 -> same as 3, but with at least one zero at band-center
* [z] -> zero locations in complex form
I would like this or similar json output
{
code:
header :
content :
}
In fact, as long as code and other content blogs are separated, there is no problem.
In addition to the above, I can even write my own converter with nodejs, but it can take a very long time.
Thanks in advance
There is a very straightforward approach incase you are working on rasa.
import rasa_nlu
from rasa_nlu.training_data import load_data
input_training_file_md_file = "nlu_data.md"
output_json_file = 'nlu_data.json'
with open(output_json_file,'w') as f:
f.write(load_data(input_training_file_md_file).as_json())
#done
You mentioned you Google'd this, so I'm sure you saw this, but for a working example of this in python, check out: https://github.com/njvack/markdown-to-json
Beyond that, I recommend reading the README for the project on some things you're going to run into. Ultimately, they're not the same type of format, so the conversion isn't reversible without guess work. Notice how all of the packages are a variety of responses. They're inventing a JSON key-value response structure to make things semi understandable.
If existing tools don't do what you need, try checking out what they're doing, and change it to your needs.

Binary - Hexa - Integer - Data cast and processing

I'm looking for a Python3 lib or an implemented way to process on binary data.
The example will tell you more than words :
first part of the packet:
packet.data1 = '0x0EDD'
I want to separate the beginning from the 2 last bits so i use the arrays methodes:
my_id = int(bin(int(packet.data1,16))[-2:],2)
my_len_of_len = int(bin(int(packet.data1,16))[:-2],2)
Now in the second part if my_len_of_len equals 1 i have to catch the following byte like :
packet.data2 = '0x08'
And then i have to convert it to int to know the number of bytes are following, they are the content of the message:
my_len = int(packet.data2,16)
And now i can catch the message from the data. I'm trying to understand a game protocol but with the methods i know, it's "slow" and hard to find myself with all array indices.
A solution for me ?
Thank you.
I recommend using a library for this. My personal recommendation is the awesome construct library.
Methods such as what you've started with, using bit-wise operators or using struct.unpack are highly error-prone and difficult to maintain.

Can the Python read() command be structured with two input variables?

I've been given some python code (at least I was told it was in python and it doesn't match matlab code structure) to get running and one of the lines is
data = f.read(1024x1024, 'int32')
I'm getting a syntax error which doesn't surprise me as I thought read() could only take one input and that was size...
I checked the docs https://docs.python.org/2/tutorial/inputoutput.html
and had a general look around, for example here:
http://www.tutorialspoint.com/python/python_files_io.htm and here: http://pymbook.readthedocs.org/en/latest/file.html
There are no indications that read() can take two inputs, nevermind one with a 'x' in it.
(I am also not clear on what the intentions of the 1024x1024 was, which is why I'm questioning if it's python, it looks like they're trying to set the size but it doesn't work like that for the read method)
Does anyone know what I'm missing? (or can work out what was originally meant by the command?)
Whole script section:
f = open(filename, 'r')
out = open(outfile, 'w')
data = f.read(1024x1024, 'int32')
result = out.write(data[0:256000])
out.closed
f.closed
It's basically notes on what they want to happen in a particular section of the script but they wrote it as if it was code and I have no idea what the intention of the data line is.
It looks more like pseudocode than anything; specifying "int32" makes me think they are reading from a binary file. You probably need something like
import numpy as np
def load_array(filename, dtype="int32", shape=(1024,1024)):
return np.fromfile(filename, dtype).reshape(shape)
Your syntax error has nothing to do with the read "command". Syntax error means that the interpreter/parser couldn't make sense of what you're writing at all. When that happens in python it will normally point at what's confusing the interpreter, fx:
data = f.read(1024x1024, 'int32')
^
SyntaxError: invalid syntax
Note the ^ pointing at 1024x1024 which is the fault, it simply doesn't understand what 1024x1024 is (so it won't get to the point to actually try to call the read method). If you meant to multiply the numbers you should have written 1024*1024 instead.
When you change to 1024*1024 you'll get other errors (for not reading the documentation for read - it doesn't take those arguments).
As for the language I'd suspect that there's no sane language with such a construct. The problem here is that x doesn't work well as a multiplication operator since that would be problematic with things like axe (did he mean a*e or the variable named axe?). It looks more like it's pseudo code.

Categories

Resources