GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Home
  • Archives
  • ABOUT

This view

  • Original page
  • New address

GeezerVison is showing coderming.github.io — M's BLOG

  • Home
  • Archives
  • ABOUT
  • Categories
  • tech
  • school

浅析 protobuf 编码解码的实现

xxx

2020/09/04 posted in  tech

Timing Attack的原理和实用库 cryptiles

最近在刷node的基础知识,偶然间看到了Node中的“Timing Attack”问题。于是尝试深入了解了一下,就有了这篇文章👇

什么是 Timing Attack?cryptiles 是做什么用的?

Timing Attack,直译过来就是“时间攻击”。是利用了语言引擎比对两个字符串的实现方法来进行的攻击。
对于JS引擎,匹配两个字符串是否相等,是做以下两件事:
- 查看两个字符串长度是否有为0的,如果都为0就返回true,只有一个为0就返回false
- 从字符串的第0个元素开始,比对两个字符串对应的字符是否相同。如果不同,就返回false。这样一直运行到两个字符串都结尾,如果结尾的标志符(在C语言中是'\0',在JS的引擎实现不清楚)都一样的话,就返回true
对于这个实现算法,我们知道其运行时间并非稳定的。因为开头正确位数不一样,所以匹配时间也不一样。而Timing Attack就是利用了匹配时间上的差距,来猜测匹配的字符串对了几位。

有什么解决方案?

Node官方自带的 crypto模块就有解决方案。具体的API可以看这里:crypto | Node.js API 文档(目前只有英文)
那 cryptiles 是做什么的呢?就是一个避免 Timing Attack 的工具。具体请看:
GitHub - hapijs/cryptiles: General purpose crypto utilities

有了这些工具,就可以很显著地避免这个问题了。

2020/06/23 posted in  tech

浅析阿里2018秋招前端编程测验题

先上题

// 实现一个函数,传入一个promise数组。要求数组中的函数按顺序依次执行,最后函数的返回值是一个由数组元素中各个promise返回值组成的数组
const timeout = ms => new Promise((resolve, reject) => {
    setTimeout(() => {
        resolve();
    }, ms);
});

const ajax1 = () => timeout(2000).then(() => {
    console.log('1');
    return 1;
});

const ajax2 = () => timeout(1000).then(() => {
    console.log('2');
    return 2;
});

const ajax3 = () => timeout(2000).then(() => {
    console.log('3');
    return 3;
});

const mergePromise = ajaxArray => {
  // 填写此处的代码
};

mergePromise([ajax1, ajax2, ajax3]).then(data => {
    console.log('done');
    console.log(data); // data 为 [1, 2, 3]
});

分析

可以看到这个题是考我们对于promise的理解,算是较为基础的题。
首先我们看到最后函数执行完成后是一个then方法。那么我们就可以知道,mergePromise函数的返回值是一个promise。那么什么东西执行后会返回一个promise呢?无非就两种:一种是promise自身,一种是async function。
其次,我们看到传入mergePromise函数的参数,是一个长度不定的数组。处理长度不定的数组的各个元素,无非就是循环或者类循环了。即forEach,for循环这些。
根据上面两点,我想到了用async function。

本人的解法

  return (async function() {
    let index = 0, length = ajaxArray.length, dataArr = []
    while(index !== length) {
      dataArr.push(await ajaxArray[index++]())
    } 
    return dataArr
  })()
2020/06/23 posted in  school

M's BLOG

keep active, keep motivate.
Email RSS

Categories

tech school

Recent Posts

  • 浅析 protobuf 编码解码的实现
  • Timing Attack的原理和实用库 cryptiles
  • 浅析阿里2018秋招前端编程测验题

Copyright © 2015 Powered by MWeb,  Theme used GitHub CSS.

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.