I am getting 401 unauthorised exception while downloading the file in byte array

using (WebClient client = new WebClient()) { client.Headers.Add(HttpRequestHeader.ContentType, "application/json;odata=verbose"); client.Headers.Add(HttpRequestHeader.Accept, "application/json;odata=verbose"); client.Headers.Add(HttpRequestHeader.ContentEncoding, "UTF-8"); client.Headers.Add("Authorization", "Bearer" + accessToken); client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f"); client.Credentials = credential; Uri endpointUri = new Uri(webUrl + "/_api/web/GetFileByServerRelativeUrl('" + folderServerRelativeUrl + "/" + fileName + "')/$  value"); //string result = client.DownloadString(endpointUri); byte[] data = client.DownloadData(endpointUri); FileStream outputStream = new FileStream(path + fileName, FileMode.OpenOrCreate | FileMode.Append, FileAccess.Write, FileShare.None); outputStream.Write(data, 0, data.Length); outputStream.Flush(true); outputStream.Close(); }  

I am getting exception at byte[] data = client.DownloadData(endpointUri);. The endpoint Uri I have generated, its working fine when I’m hitting it in browser or using POSTMAN. I am able to download the file using endpoint Uri, its just not working through code.

I’m lost and couldn’t find anything. Any help will be useful for me.

Mono criando um dicionario de int e byte[]

Eu estou criando um projeto em C++ e utilizando a mono para trabalhar em conjunto com outro programa em C#, porem eu me deparei com um pequeno problema, eu preciso criar um dicionario de <int,byte[]> no C++ e passar ele para o programa em C#, preferencialmente eu não queria ter que criar uma função no projeto em C# para criar o dicionario, queria fazer ele no meu projeto em C++ e só passar para o projeto em C#, existe alguma forma de fazer isso?

Tensorflow js loadLayersModel range error on node 8.10/aws lambda “byte length of Float32Array should be a multiple of 4”, works on browser?

I have trained and saved an image classification CNN using tensorflow js following the browser example provided.

https://codelabs.developers.google.com/codelabs/tfjs-training-classfication/index.html#6

Then I uploaded my model’s json and bin file to a public s3 bucket, and tried to load it like so in a lambda so that it can later on make and return a prediction when I call model.predict().

var tf = require('@tensorflow/tfjs'); global.fetch = require('node-fetch');  exports.handler = async function(event, context) {     var data = typeof event === String ? JSON.parse(event) : event;      var shidDataItem = data.shipDataItem;     var image = predictionData.tripImage;      console.log('getting model');     const model = await tf.loadLayersModel('https://s3.amazonaws.com/{my_bucket}/my-model.json');     console.log(model); } 

But when I test this I get the following error –

{   "errorMessage": "byte length of Float32Array should be a multiple of 4",   "errorType": "RangeError",   "stackTrace": [     "typedArrayConstructByArrayBuffer (<anonymous>)",     "new Float32Array (native)",     "_loop_1 (/var/task/node_modules/@tensorflow/tfjs-core/dist/io/io_utils.js:159:30)",     "Object.decodeWeights (/var/task/node_modules/@tensorflow/tfjs-core/dist/io/io_utils.js:189:9)",     "/var/task/node_modules/@tensorflow/tfjs-layers/dist/models.js:298:50",     "step (/var/task/node_modules/@tensorflow/tfjs-layers/dist/models.js:54:23)",     "Object.next (/var/task/node_modules/@tensorflow/tfjs-layers/dist/models.js:35:53)",     "fulfilled (/var/task/node_modules/@tensorflow/tfjs-layers/dist/models.js:26:58)",     "<anonymous>",     "process._tickDomainCallback (internal/process/next_tick.js:228:7)"   ] } 

However, this snippet works on the browser –

const model = await tf.loadLayersModel('https://s3.amazonaws.com/{my_bucket}/my-model.json'); 

Has anyone encountered this issue? Any suggestions for loading a model in a lambda to make a quick prediction? Thanks for your time.

How to convert a zip to byte array and read it as a stream (not loading the whole file to memory)?

I have an LZMA zip archive that I want to decompress using LZMA-JS, the issue is that the decompress method only accepts a byte array or node Buffer object and I do not know how to convert my zip file to a buffer object without loading the whole file to memory – so its best if it is some kind of read stream as the files I am trying to convert are huge.

It would be great if someone could suggest a way to create a byte array stream from zip file using Javascript – thank you so much! 🙂

Converting hex strings to byte arrays and back to hex strings in Java

Here is my attempt at converting hex strings to byte arrays and converting byte arrays to hex strings:

net.coderodde.util.ByteStringConverter

package net.coderodde.util;  import java.util.Scanner;  public class ByteStringConverter {      /**      * Converts the given byte array to its textual hex representation.      * @param bytes the byte array to stringify.      * @return the string representing the input byte array.      */     public static String convertByteArrayToHexString(byte[] bytes) {         StringBuilder stringBuilder = new StringBuilder(2 * bytes.length);          for (byte b : bytes) {             stringBuilder.append(convertByteToHexString(b));         }          return stringBuilder.toString();     }      /**      * Converts the given hex string to the byte array it represents.      * @param hexString the hex string to convert.      * @return the byte array represented by the input hex string.      */     public static byte[] convertHexStringToByteArray(String hexString) {         byte[] byteArray = new byte[hexString.length() / 2];          for (int i = 0; i < hexString.length(); i += 2) {             byteArray[i / 2] = convertHexByteStringToByte(                     hexString.substring(i, i + 2));         }          return byteArray;     }      /**      * Converts the input character {@code c} to the nibble it represents. This      * method assumes that {@code c} is numeric or within range       * {@code a, b, c, d, e, f}.      * @param c the character to convert to a nibble.      * @return the byte value of the textual representation of a nibble.      */     private static byte convertHexCharToNibble(char c) {         c = Character.toLowerCase(c);          switch (c) {             case '0':                 return 0;             case '1':                 return 1;             case '2':                 return 2;             case '3':                 return 3;             case '4':                 return 4;             case '5':                 return 5;             case '6':                 return 6;             case '7':                 return 7;             case '8':                 return 8;             case '9':                 return 9;             case 'a':                 return 10;             case 'b':                 return 11;             case 'c':                 return 12;             case 'd':                 return 13;             case 'e':                 return 14;             case 'f':                 return 15;              default:                 throw new IllegalArgumentException("Not a hex digit: " + c);         }     }      /**      * Converts the input hex byte string to the byte it represents.      * @param hexByteString the hex byte string to convert.      * @return the byte value represented by {@code hexByteString}.      */     private static byte convertHexByteStringToByte(String hexByteString) {         char lo = hexByteString.charAt(1);         char hi = hexByteString.charAt(0);         byte lob = convertHexCharToNibble(lo);         byte hib = convertHexCharToNibble(hi);         return (byte)((hib << 4) | lob);     }      /**      * Converts the given byte to its textual hex representation.      * @param b the byte to convert.      * @return the textual representation of the byte {@code b}.      */     private static String convertByteToHexString(byte b) {         byte lo = (byte)(b & (byte) 0xf);         byte hi = (byte)((b >>> 4) & (byte) 0xf);         StringBuilder stringBuilder = new StringBuilder(2);         appendNibbleToStringBuilder(stringBuilder, hi);         appendNibbleToStringBuilder(stringBuilder, lo);         return stringBuilder.toString();     }      /**      * Appends the textual representation of {@code nibble} to       * {@code stringBuilder}.      * @param stringBuilder the target string builder.      * @param nibble the nibble to append.      */     private static void appendNibbleToStringBuilder(StringBuilder stringBuilder,                                                      byte nibble) {         switch (nibble) {             case 0:                 stringBuilder.append('0');                 break;             case 1:                 stringBuilder.append('1');                 break;             case 2:                 stringBuilder.append('2');                 break;             case 3:                 stringBuilder.append('3');                 break;             case 4:                 stringBuilder.append('4');                 break;             case 5:                 stringBuilder.append('5');                 break;             case 6:                 stringBuilder.append('6');                 break;             case 7:                 stringBuilder.append('7');                 break;             case 8:                 stringBuilder.append('8');                 break;             case 9:                 stringBuilder.append('9');                 break;             case 10:                 stringBuilder.append('a');                 break;             case 11:                 stringBuilder.append('b');                 break;             case 12:                 stringBuilder.append('c');                 break;             case 13:                 stringBuilder.append('d');                 break;             case 14:                 stringBuilder.append('e');                 break;             case 15:                 stringBuilder.append('f');                 break;         }     }      public static void main(String[] args) {         Scanner scanner = new Scanner(System.in);          while (true) {             String hexString = scanner.next();             byte[] hexBytes = convertHexStringToByteArray(hexString);             String newHexString = convertByteArrayToHexString(hexBytes);             System.out.println(newHexString);         }     } } 

As always, any critique is much appreciated!

Correct way to get a number from 0-9 from a random byte?

If I have a good random number generator that gives me a byte of data at a time, and I want to extract a random decimal digit of 0 to 9 from that byte stream, what is the correct way to do that?

At first I naively assumed that a simple (randomByte mod 10) calculation would be sufficient, but since 256 is not evenly divisible by 10, that results in a clear bias in the “random” digits:

0: 101323 ################################# 1: 101261 ################################# 2: 101473 ################################# 3: 101389 ################################# 4: 101551 ################################# 5: 101587 ################################# 6: 97831  ############################### 7: 97893  ############################### 8: 97843  ############################### 9: 97849  ############################### (histogram from 1 million 'random' digits) 

One method that appears to work is to discard any value above 249 and divide by 25. Is that cryptographically correct? Is there a better method that doesn’t involve discarding (potentially expensive) bytes of randomness?

(this question is prompted by reading about a CryptoCat vulnerability, where one of the discovered flaws was that they discarded random values above 250 instead of above 249, giving a slight bias in their “random” numbers… so I was curious what the “right” way to do it is)

Why are WPA2 passwords longer than 16 bytes more secure than 16 byte passwords?

In this good Information Security StackExchange question, the answers reveal that a long WPA2-PSK password does not degrade performance of the network. The reasoning is that the password itself is never transmitted, and the generated CMAC that is transmitted is always 128 bits (16 bytes).

Why then, are WPA2 passwords longer than 16 bytes more secure than WPA2 passwords of 16 bytes?