<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[M's BLOG]]></title>
  <link href="coderming.github.io/atom.xml" rel="self"/>
  <link href="coderming.github.io/"/>
  <updated>2020-09-04T13:04:01+08:00</updated>
  <id>coderming.github.io/</id>
  <author>
    <name><![CDATA[]]></name>
    
  </author>
  <generator uri="http://www.mweb.im/">MWeb</generator>
  
  <entry>
    <title type="html"><![CDATA[浅析 protobuf 编码解码的实现]]></title>
    <link href="coderming.github.io/15991923052013.html"/>
    <updated>2020-09-04T12:05:05+08:00</updated>
    <id>coderming.github.io/15991923052013.html</id>
    <content type="html"><![CDATA[
<p>xxx</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Timing Attack的原理和实用库 cryptiles]]></title>
    <link href="coderming.github.io/15929046514995.html"/>
    <updated>2020-06-23T17:30:51+08:00</updated>
    <id>coderming.github.io/15929046514995.html</id>
    <content type="html"><![CDATA[
<p>最近在刷node的基础知识，偶然间看到了Node中的“Timing Attack”问题。于是尝试深入了解了一下，就有了这篇文章👇</p>

<h2 id="toc_0">什么是 Timing Attack？cryptiles 是做什么用的？</h2>

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

<h2 id="toc_1">有什么解决方案？</h2>

<p>Node官方自带的 <code>crypto</code>模块就有解决方案。具体的API可以看这里：<a href="http://nodejs.cn/api/crypto.html#crypto_crypto_timingsafeequal_a_b">crypto | Node.js API 文档</a>（目前只有英文）<br/>
那 cryptiles 是做什么的呢？就是一个避免 Timing Attack 的工具。具体请看：<br/>
<a href="https://github.com/hapijs/cryptiles">GitHub - hapijs/cryptiles: General purpose crypto utilities</a></p>

<p>有了这些工具，就可以很显著地避免这个问题了。</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[浅析阿里2018秋招前端编程测验题]]></title>
    <link href="coderming.github.io/15928819115606.html"/>
    <updated>2020-06-23T11:11:51+08:00</updated>
    <id>coderming.github.io/15928819115606.html</id>
    <content type="html"><![CDATA[
<h3 id="toc_0">先上题</h3>

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

const ajax1 = () =&gt; timeout(2000).then(() =&gt; {
    console.log(&#39;1&#39;);
    return 1;
});

const ajax2 = () =&gt; timeout(1000).then(() =&gt; {
    console.log(&#39;2&#39;);
    return 2;
});

const ajax3 = () =&gt; timeout(2000).then(() =&gt; {
    console.log(&#39;3&#39;);
    return 3;
});

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

mergePromise([ajax1, ajax2, ajax3]).then(data =&gt; {
    console.log(&#39;done&#39;);
    console.log(data); // data 为 [1, 2, 3]
});
</code></pre>

<h3 id="toc_1">分析</h3>

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

<h3 id="toc_2">本人的解法</h3>

<pre><code class="language-javascript">  return (async function() {
    let index = 0, length = ajaxArray.length, dataArr = []
    while(index !== length) {
      dataArr.push(await ajaxArray[index++]())
    } 
    return dataArr
  })()
</code></pre>

]]></content>
  </entry>
  
</feed>
