/* Apply a mask (repeated if shorter than message) as XOR to each byte.  */
static int crypto_mask(lua_State *L)
/* Apply a mask (repeated if shorter than message) as XOR to each byte.  */
static int crypto_mask(lua_State *L)
{
  int len, mask_len, i;
  const char* msg = luaL_checklstring(L, 1, &len);
  const char* mask = luaL_checklstring(L, 2, &mask_len);
  luaL_Buffer b;
  if(mask_len <= 0)
    return luaL_error(L, "invalid argument: mask");
  luaL_buffinit(L, &b);
  for (i = 0; i < len; i++) {
    luaL_addchar(&b, msg[i] ^ mask[i % mask_len]);
  }
  luaL_pushresult(&b);
  return 1;
}