I am trying to encode message (containing openssl signature) in base64 string. In NodeJS I am using Buffers:
var encodedMessage = new Buffer(message).toString('base64');
In PHP I am using php_openssl extension:
$encodedMessage = base64_encode($message);
Messages before encoding
are the same have same VISUAL REPRESENTATION in console. But when I encode them I get different results.
I tested two messages with a third-party web service. NodeJS message was rejected, PHP worked like a charm.
So I have to adjust encoding in NodeJS to be like in PHP. How can I do that?
I am using NodeJS version is 4.4.5, PHP version is 7.1.3
In NodeJS I was constructing signed message like this:
var message = 'I am a simple message';var sign = crypto.createSign('SHA1');var signature = '';sign.update(message);signature = sign.sign(privateKey);message += signature;
In PHP code looks like this:
$message ='I am a simple message';openssl_sign($message, $signature, $pkeyid); $message .= $signature;
When I printed the message content (
echo $message) I received same result. But when I encoded the message to base64 string I got two different outputs.
After that I hard-coded the message (before base64 encoding) got same result in NodeJS and PHP.
That's why I am assuming that the problem is connected with signature type (and encoding) and this line:
message += signature;
I created two Buffers (one for message, one for signature), concatenated them and encode the result base64 string.
var signedMsgBuffer = Buffer.concat([messageBuffer, signBuffer]);var result = signedMsgBuffer.toString('base64');
Could you someone explain this behavior?
I'm working on an app that to do some facial recognition from a webcam stream. I get base64 encoded data uri's of the canvas and want to use it to do something like this:
The data URI looks something like this:
So, for clarity I've shown what the image looks like so the base64 string is not broken.
import base64 imgdata = base64.b64decode(imgstring) #I use imgdata as this variable itself in references below filename = 'some_image.jpg' with open(filename, 'wb') as f: f.write(imgdata)
The above code snippet works and the image file gets generated properly. However I don't think so many File IO operations are feasible considering I'd be doing this for every frame of the stream. I want to be able to read the image into the memory directly creating the
I have tried two solutions that seem to be working for some people.
Using PIL reference:
pilImage = Image.open(StringIO(imgdata))npImage = np.array(pilImage)matImage = cv.fromarray(npImage)
cv not defined as I have openCV3 installed which is available to me as
cv2 module. I tried
img = cv2.imdecode(npImage,0), this returns nothing.
Getting the bytes from decoded string and converting it into an numpy array of sorts
file_bytes = numpy.asarray(bytearray(imgdata), dtype=numpy.uint8)img = cv2.imdecode(file_bytes, 0) #Here as well I get returned nothing
The documentation doesn't really mention what the
imdecode function returns. However, from the errors that I encountered, I guess it is expecting a
numpy array or a
scalar as the first argument. How do I get a handle on that image in memory so that I can do
cv2.imshow('image',img) and all kinds of cool stuff thereafter.
I hope I was able to make myself clear.
I've been reading about base64 conversion, and what I understand is that the encoded version of the original data will be 133% of the original size.
Then, I'm reading about how YouTube is able to have unique identifiers to their videos like
FJZQSHn7fc and the reason was: an 11 character base64 string can map to a huge number.
Wait, say a huge number contains 20 characters, then wouldn't a base64 encoded string be 133% of that size, not shorter?
I'm very confused. Are there different types of base64 conversion (string to base64 vs. decimal to base64), once resulting in a bigger, and the other in a smaller resulting string?
The purpose of the
base64.b64encode() function is to convert binary data into ASCII-safe "text". However the return type of the method is a bytes object, e.g.:
Python 3.5.2 (default, Nov 17 2016, 17:05:23)[GCC 5.4.0 20160609] on linuxType "help", "copyright", "credits" or "license" for more information.>>> import base64>>> base64.b64encode(b'abc')b'YWJj'
Now, it's easy to simply take that output and
decode() it, but my question is: What is a significance of
base64.b64encode() returning a
bytes object rather than a
I am trying to encode a text string to base64.
i tried doing this :
name = "your name"print('encoding %s in base64 yields = %s\n'%(name,name.encode('base64','strict')))
But this gives me the following error:
LookupError: 'base64' is not a text encoding; use codecs.encode() to handle arbitrary codecs
How do I go about doing this ? ( using Python 3.4)