binary-to-text encoding, non-printing characters, protocol buffers, mongodb and bson - c++

I have a candidate key (mongodb candidate key, __id) thats looks like the following in protocol buffers :
message qrs_signature
{
required uint32 region_id = 1;
repeated fixed32 urls = 2;
};
Naturally I can't use a protocol buffers encoded string (via ParseToString(std::string)) in my bson document since it can contain non-printing characters. Therefore, I am using the ascii85 encoding to encode the data (using this library). I have two questions.
Is b85 encoding bson-safe.
What is bson's binary type for ? is there some way that I can implant my (binary) string into that field using a mongodb API call , or is it just syntactic sugar to denote a value-type that needs to be processed in some form (--i.e., not a native mongodb entity)?
edit
The append binary api's show's data being encoded as hex(OMG!), base85 is therefore more space efficient (22 bytes per record in my case).

BSON safe, yes. The output of ASCII85 encoding is also valid utf-8 iirc.
It's used to store chunks of binary data. Binary data is an officially supported type and you should be able to push binary values to BSON fields using the appropriate driver code, BSONObj in your case. Refer to your driver docs or the source code for details.

Related

CAdES signature with win32 low level message apis

I am trying to use the low level message functions to create a CAdES-BES compliant signature.
The signature has to be in proper PKCS#7 format along with four signed attributes present.
The first two, type and hash are automatically put but the crypto library.
The third one is the time of the message. I was able to put this using CryptEncodeObject and szOID_RSA_signingTime
"1.2.840.113549.1.9.5".
The fourth one is the signing certificate, which OID's is not supported by the crypto API, 1.2.840.113549.1.9.16.2.47.
To be able to add this manually I have to know how CryptEncodeObject works internally, and the sort of structure to encode:
SigningCertificateV2 ::= SEQUENCE {
certs SEQUENCE OF ESSCertIDv2,
policies SEQUENCE OF PolicyInformation OPTIONAL
}
ESSCertIDv2 ::= SEQUENCE {
hashAlgorithm AlgorithmIdentifier
DEFAULT {algorithm id-sha256},
certHash Hash,
issuerSerial IssuerSerial OPTIONAL
}
Hash ::= OCTET STRING
IssuerSerial ::= SEQUENCE {
issuer GeneralNames,
serialNumber CertificateSerialNumber
}
And more structures that have to be included before it's completed.
I could try to use an ASN.1 compiler to build the whole thing, then a DER encoder to encode it, but is there a simpler way?
I did it, you have to use an ASN.1 compiler and then encode signed attributes with the CMS.
https://www.codeproject.com/Articles/1256991/AdES-An-implementation-of-CAdES-for-Windows-in-Cpl

How can I send a list using MQTT

d = random,randint(1,30)
data = [d, strftime("%Y%m%d %H%M%S", gmtime())] #random num , system time
client.publish("gas", str(data)]
This is a part of my python code which is ver2.
I'm trying to send a list using MQTT.
However, If I write bytearray instead of str which is third line
It says "ValueError: string must be of size 1".
So I wrote str then make it sting type
Can I send a just list which is NOT string type.
MQTT message payloads are just byte arrays, there is no inherent format to them. Strings tend to works as long as both ends of the transaction are using the same character encoding.
If you want to send structured data (such as the ost) then you need to decide on a way to encode that structure so the code receiving the message will know how to reconstruct it.
The current usual solution to this problem is to encode structures are JSON, but XML or something like protobuffers are also good candidates.
The following question has some examples of converting Python lists to JSON objects
Serializing list to JSON

How to store byte array in HBase using thrift C++ API (value in Mutation Struct is Text ...)

I want store file in HBase with C++.
But HBase Thrift Api doesn't support byte array ... only support Text (string).
How do I Store file in HBase using C++?
You may have solved this by now but I had a similar issue and resolved it by constructing a string from the entire byte array that I had. And then set it as a value in the mutation.
// dbytes is the byte array
std::string valStr;
valStr.assign(std::begin(dbytes), std::end(dbytes));
mutations.back().value = valStr;

Can I force MySQL to transmit numerical values as binary?

I've been investigating network performance of our MySQL/C++ application. I found that our clients read all values (ints, doubles, etc.) from the MYSQL C-Connector in ASCII literals. That means that the server is transmitting numbers to the clients as text (eg. the String "123" for the int 123). All of our data in this application consists of numerical values (ints and doubles), so I am wondering:
Is there a way to force the MySQL server to transmit the numerical values in binary format rather than ASCII?
Use a compressed connection. Look for UseCompression in the docs

How can I obfuscate/de-obfuscate integer properties?

My users will in some cases be able to view a web version of a database table that stores data they've entered. For various reasons I need to include all the stored data, including a number of integer flags for each record that encapsulate adjacencies and so forth within the data (this is for speed and convenience at runtime). But rather than exposing them one-for-one in the webview, I'd like to have an obfuscated field that's just called "reserved" and contains a single unintelligible string representing those flags that I can easily encode and decode.
How can I do this efficiently in C++/Objective C?
Thanks!
Is it necessary that this field is exposed to the user visually, or just that it’s losslessly captured in the HTML content of the webview? If possible, can you include the flags as a hidden input element with each row, i.e., <input type=“hidden” …?
Why not convert each of the fields to hex, and append them as a string and save that value?
As long as you always append the strings in the same order, breaking them back apart and converting them back to numbers should be trivial.
Use symmetric encryption (example) to encode and decode the values. Of course, only you should know of the key.
Alternatively, Assymetric RSA is more powerfull encryption but is less efficient and is more complex to use.
Note: i am curios about the "various reasons" that require this design...
Multiply your flag integer by 7, add 3, and convert to base-36. To check if the resulting string is modified, convert back to base-2, and check if the result modulo 7 is still 3. If so, divide by 7 to get the flags. note that this is subject to replay attacks - users can copy any valid string in.
Just calculate a CRC-32 (or similar) and append it to your value. That will tell you, with a very high probability, if your value has been corrupted.