Related
I am unable to decrypt this Uint8Array
Uint8Array(32) [
174, 157, 255, 238, 54, 143, 97, 132,
70, 243, 7, 249, 98, 188, 68, 170,
53, 82, 78, 9, 96, 226, 182, 160,
131, 79, 3, 147, 153, 34, 205, 162
]
using this code
const string = new TextDecoder().decode(hash);
console.log(string);
I get this: ����Kc2Jk�&an↕�9���&��h-Lﺠ�Ԋ
I've also tried with many online converters but it says there's some problem with the byte array. This byte array is a response from an API.
Where Am I wrong? How can I convert it properly?
I have a simple program which is decompressing a byte array in C#.
There is the code of "Decompress" func:
private byte[] Decompress(byte[] compressed)
{
using var from = new MemoryStream(compressed);
using var to = new MemoryStream();
using var gZipStream = new GZipStream(from, CompressionMode.Decompress);
gZipStream.CopyTo(to);
return to.ToArray();
}
and I have two byte arrays
var first = new byte[] { 31, 139, 8, 0, 0, 0, 0, 0, 0, 10, 45, 202, 65, 10, 128, 48, 12, 5, 209, 171, 132, 28, 195, 219, 68, 251, 91, 11, 38, 41, 105, 92, 137, 119, 23, 197, 221, 240, 152, 139, 39, 80, 120, 185, 88, 13, 234, 214, 55, 94, 88, 197, 154, 83, 115, 73, 154, 103, 84, 217, 64, 56, 48, 118, 177, 164, 130, 57, 164, 7, 5, 20, 186, 226, 141, 22, 72, 90, 97, 168, 61, 41, 251, 167, 7, 124, 72, 20, 250, 167, 233, 145, 124, 223, 15, 199, 8, 21, 161, 110, 0, 0, 0 };
var second = new byte[] { 31, 139, 8, 0, 0, 0, 0, 0, 0, 10, 1, 80, 0, 175, 255, 34, 127, 33, 66, 194, 252, 100, 6, 77, 155, 56, 146, 208, 225, 255, 219, 203, 33, 122, 153, 14, 246, 130, 28, 163, 183, 127, 145, 96, 71, 208, 206, 123, 9, 122, 133, 204, 255, 86, 191, 73, 197, 91, 247, 121, 156, 180, 101, 176, 125, 158, 123, 114, 192, 252, 70, 193, 148, 180, 183, 200, 146, 79, 121, 135, 152, 90, 247, 107, 168, 157, 128, 84, 121, 4, 122, 92, 145, 45, 223, 177, 179, 189, 149, 80, 0, 0, 0 }
my next step is:
var decompressed = Decompress(first);
var result = Encoding.UTF8.GetString(decompressed);
Output: {"seed":{"mnemonic":"mango goat surface elephant despair remember regret benefit timber leopard member sort"}}
var decompressed = Decompress(second);
var result = Encoding.UTF8.GetString(decompressed);
Output: "!B??d♠M?8??????!z???∟???`G??{ z???V?I?[?y??e?}?{r??F?????Oy??Z?k???Ty♦z\?-?
The structure of these arrays is the same... length (103), first 10 elements and last 3 elements. But first array decompresses with func Decompress fine and the second one is very bad. I don't know why it's happening. Someone explain where is my mistake.
I tried to decompress with python using gzip but I have the same result. There is the code
import gzip
first = [31, 139, 8, 0, 0, 0, 0, 0, 0, 10, 45, 202, 65, 10, 128, 48, 12, 5, 209, 171, 132, 28, 195, 219, 68, 251, 91, 11, 38, 41, 105, 92, 137, 119, 23, 197, 221, 240, 152, 139, 39, 80, 120, 185, 88, 13, 234, 214, 55, 94, 88, 197, 154, 83, 115, 73, 154, 103, 84, 217, 64, 56, 48, 118, 177, 164, 130, 57, 164, 7, 5, 20, 186, 226, 141, 22, 72, 90, 97, 168, 61, 41, 251, 167, 7, 124, 72, 20, 250, 167, 233, 145, 124, 223, 15, 199, 8, 21, 161, 110, 0, 0, 0 ];
second = [31, 139, 8, 0, 0, 0, 0, 0, 0, 10, 1, 80, 0, 175, 255, 34, 127, 33, 66, 194, 252, 100, 6, 77, 155, 56, 146, 208, 225, 255, 219, 203, 33, 122, 153, 14, 246, 130, 28, 163, 183, 127, 145, 96, 71, 208, 206, 123, 9, 122, 133, 204, 255, 86, 191, 73, 197, 91, 247, 121, 156, 180, 101, 176, 125, 158, 123, 114, 192, 252, 70, 193, 148, 180, 183, 200, 146, 79, 121, 135, 152, 90, 247, 107, 168, 157, 128, 84, 121, 4, 122, 92, 145, 45, 223, 177, 179, 189, 149, 80, 0, 0, 0 ];
bytes_of_values = bytes(first)
decompressed_block = gzip.decompress(bytes_of_values)
print(decompressed_block.decode('utf8', 'ignore'))
#Output: {"seed":{"mnemonic":"mango goat surface elephant despair remember regret benefit timber leopard member sort"}}
bytes_of_values = bytes(second)
decompressed_block = gzip.decompress(bytes_of_values)
print(decompressed_block.decode('utf8', 'ignore'))
#Output: "!Bd♠M8!z∟`G{ zVI[ye}{rFȒOyZkTy♦z\-
UPDATE #1
FIRST ARRAY was expanded with this JS function.
const crypto = require('crypto')
module.exports = function (size ) {
return {
expand (data ) {
const buffer = crypto.randomBytes(data.length < size - 4 ? size : data.length + 4)
buffer.writeUInt32BE(data.length, 0)
data.copy(buffer, 4, 0)
return buffer
},
shrink (buffer ) {
const dataLen = buffer.readUInt32BE(0)
return buffer.slice(4, dataLen + 4)
}
}
}
Where size = 2 ** 15 (2^15 = 32768); data = {"seed":{"mnemonic":"mango goat surface elephant despair remember regret benefit timber leopard member sort"}}. In output we have a Buffer with size 32768. After that I take this Buffer and convert to string like "[1, 2, 3 ,4 ,5, etc.]" (remind that size is 32768) and pass this to C#. In C# I use this code:
var shrink = BinaryPrimitives.ReadUInt32BigEndian(data); //data == array with size 32768 (string from JS)
//shrink == 103 (integer)
var segment = new ArraySegment<byte>(data, 4, (int)shrink).ToArray();
// segment == FIRST ARRAY
after following these steps I get the FIRST ARRAY and it decompresses fine. The structure of the first array are identical to SECOND ARRAY. And I understood that SECOND ARRAY uses the same algorithm. But it doesn't unpack.
I am not familiar with TensorFlow.js. I am creating a module in React and Node.js with TensorFlow.js.
I am able to capture image first and convert image to Tensor with this code:
const imageDataURL = this.canvasRef.current.toDataURL('image/png');
const b = Buffer.from(imageDataURL, 'base64');
getting result:
Uint8Array(297020) [117, 171, 90, 138, 102, 160, 123, 250, 103, 129, 182, 172, 123, 174, 34, 84, 19, 145, 195, 66, 134, 130, 128, 0, 0, 3, 82, 82, 17, 20, 128, 0, 0, 150, 0, 0, 0, 125, 2, 1, 128, 0, 0, 56, 84, 20, 245, 0, 0, 8, 0, 18, 81, 16, 85, 30, 23, 187, 47, 90, 100, 201, 50, 92, 73, 90, 59, 132, 94, 89, 199, 119, 67, 71, 136, 185, 32, 185, 43, 50, 34, 28, 25, 143, 243, 63, 190, 198, 68, 144, 40, 211, 192, 136, 57, 242, 242, 43, 59, 20, …]
And with this code:
const tensor = tf.browser.fromPixels(this.videoRef.current);
result is:
Tensor {kept: false, isDisposedInternal: false, shape: Array(3), dtype: "int32", size: 921600, …}
dataId: {id: 4811}
dtype: "int32"
id: 3541
isDisposedInternal: false
kept: false
rankType: "3"
scopeId: 6508
shape: (3) [480, 640, 3]
size: 921600
strides: (2) [1920, 3]
isDisposed: (...)
rank: (...)
__proto__: Object
I need to convert the first image to Tensor, after that I need to match the face with real-time webcam: is person matching, true or false.
I am very confused on how to write the logic.
i am building a blockchain on hyperledger fabric (node.js SDK).
https://hyperledger.github.io/fabric-sdk-node/release-1.4/global.html#BlockchainInfo__anchor
i call the api BlockchainInfo and get a json format response, which includes currentBlockHash
the offical document shows that the response data type of currentBlockHash is Array< byte >
such as the following data.
i would like to convert this buffer to string but have no idea what to do.
thanks for your reading the question.
{ buffer:
{ type: 'Buffer',
data:
[ 8,
207,
230,
17,
18,
32,
124,
143,
73,
40,
171,
42,
251,
237,
193,
138,
36,
92,
58,
57,
254,
56,
144,
96,
54,
201,
242,
64,
10,
111,
150,
28,
198,
187,
196,
118,
97,
160,
26,
32,
16,
160,
154,
19,
11,
179,
147,
11,
38,
16,
150,
190,
126,
17,
121,
123,
200,
7,
71,
27,
241,
103,
54,
188,
196,
248,
178,
88,
48,
115,
186,
133 ] },
offset: 6,
markedOffset: -1,
limit: 38,
littleEndian: true,
noAssert: false }
here is the origin response data
{"height":{"low":291663,"high":0,"unsigned":true},"currentBlockHash":{"buffer":{"type":"Buffer","data":[8,207,230,17,18,32,124,143,73,40,171,42,251,237,193,138,36,92,58,57,254,56,144,96,54,201,242,64,10,111,150,28,198,187,196,118,97,160,26,32,16,160,154,19,11,179,147,11,38,16,150,190,126,17,121,123,200,7,71,27,241,103,54,188,196,248,178,88,48,115,186,133]},"offset":6,"markedOffset":-1,"limit":38,"littleEndian":true,"noAssert":false},"previousBlockHash":{"buffer":{"type":"Buffer","data":[8,207,230,17,18,32,124,143,73,40,171,42,251,237,193,138,36,92,58,57,254,56,144,96,54,201,242,64,10,111,150,28,198,187,196,118,97,160,26,32,16,160,154,19,11,179,147,11,38,16,150,190,126,17,121,123,200,7,71,27,241,103,54,188,196,248,178,88,48,115,186,133]},"offset":40,"markedOffset":-1,"limit":72,"littleEndian":true,"noAssert":false}}
I think the JSDoc is misleading in this case. I think the currentBlockHash will actually be a Node.js Buffer (or possibly a protobuf implementation that emulates the behaviour of a Buffer). You can convert a Buffer into a string by calling currentBlockHash.toString().
I have observed some cases where calling toString() on a protobuf Buffer implementation gives a debug string rather than a string representation of the content of the buffer so I tend to specify an encoding argument just to be safe.
In the case of a hash it may be more convenient to see it as a hex string rather than utf8 (which would be the default) anyway, so I would try currentBlockHash.toString('hex')
See here for more information on Buffers: https://nodejs.org/docs/latest-v10.x/api/buffer.html
I'm trying to make an audio analyzer with bars on an HTML5 canvas with the Web Audio API, showing frequencies on the x-axis and magnitudes on y-axis. I'm asking because my x-axis is in linear scale, how can I show this on a logarithmic scale?
I'm using an array Uint8Array like this var a = [32, 43, 46, 47, 46, 44, 54, 65, 67, 69, 68, 73, 64, 77, 76, 88, 92, 95, 146, 198, 217, 216, 194, 134, 94, 97, 79, 85, 71, 69, 61, 63, 60, 56, 50, 61, 56, 54, 52, 51, 53, 58, 59, 52, 45, 45, 40, 40, 32, 32, 35, 38, 31, 18, 5, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...];
These are the 99 first elements of an array of fftSize.length of 2048, the audio is a 440Hz tone;
I don't know what means "scale".
I've created a jsFiddle showing my errors
https://jsfiddle.net/jmgarciamari/3namk9y9/2/