// Puts together the body of the post request for a OTP encrypted key, // returns a string[] with the did, the signature, and the body of the // post request. public static string[] makePost(byte[] encryptedKey) { byte[] vk = new byte[32]; byte[] sk = new byte[64]; string dateTime; string body; string did; string signature; string keyString = Convert.ToBase64String(encryptedKey); int signed_bytes = LibSodiumManager.nacl_crypto_sign_BYTES(); LibSodiumManager.nacl_crypto_sign_keypair(vk, sk); did = makeDid(vk); dateTime = DateTime.Now.ToString("yyyy-MM-ddTHH\\:mm\\:ss.ffffffzzz"); body = "{\"id\":\"" + did + "\",\"blob\":\"" + keyString + "\",\"changed\":\"" + dateTime + "\"}"; byte[] bodyByte = new byte[body.Length]; bodyByte = Encoding.UTF8.GetBytes(body); byte[] sm = new byte[signed_bytes + bodyByte.Length]; signature = signResource(sm, bodyByte, (ulong)bodyByte.Length, sk, vk); signature = "signer=\"" + signature + "\""; string[] data = new string[3]; data[0] = did; data[1] = signature; data[2] = body; return(data); }
// Create signature to use in the header of POST and PUT requests to didery public static string signResource(byte[] sm, byte[] m, ulong mlen, byte[] sk, byte[] vk) { LibSodiumManager.nacl_crypto_sign(sm, m, mlen, sk); byte[] sig = new byte[LibSodiumManager.nacl_crypto_sign_BYTES()]; for (int i = 0; i < sig.Length; i++) { sig[i] = sm[i]; } byte[] usm = new byte[m.Length]; int success = LibSodiumManager.nacl_crypto_sign_open(usm, sm, (ulong)sm.Length, vk); if (success == 0) { Debug.Log("Signing successful"); } else { Debug.Log("Signing unsuccessful: " + success); } string signature = Convert.ToBase64String(sig).Replace('+', '-').Replace('/', '_'); return(signature); }
// Generates a random seed based on the size of the byte array argument passed in public static byte[] randomSeedGenerator(byte[] seed) { for (int i = 0; i < seed.Length; i++) { seed[i] = (byte)LibSodiumManager.nacl_randombytes_random(); } return(seed); }
void Test() { Debug.Log("LibSodium Test: randombytes_buf_deterministic"); int size = 16; byte[] buf = new byte[size]; byte[] seed = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f }; LibSodiumManager.nacl_randombytes_buf_deterministic(buf, size, seed); Debug.Log("Buffer Size: " + buf.Length); Debug.Log("Buffer: " + ByteArrayToString(buf)); }
// Generates the one-time pad from a seed public static void OTPGenerator(byte[] otp, int size, byte[] seed) { LibSodiumManager.nacl_randombytes_buf_deterministic(otp, size, seed); //Debug.Log("Seed length: " + seed.Length + " Seed string: " + ByteArrayToHex(seed)); //Debug.Log("OTP length: " + otp.Length + " OTP first bytes: " + otp[0] + " " + otp[1] + " " + otp[2] + " " + otp[3]); }