當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript crypto.timingSafeEqual函數代碼示例

本文整理匯總了TypeScript中crypto.timingSafeEqual函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript timingSafeEqual函數的具體用法?TypeScript timingSafeEqual怎麽用?TypeScript timingSafeEqual使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了timingSafeEqual函數的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: verifyToken

  public verifyToken(recivedToken: string, token2: string, decode = true) {
    let tokenDecoded = true;
    let expired = false;

    if (decode) {
      const enrollmentTokenSecret = this.framework.getSetting('encryptionKey');

      try {
        verifyToken(recivedToken, enrollmentTokenSecret);
        tokenDecoded = true;
      } catch (err) {
        if (err.name === 'TokenExpiredError') {
          expired = true;
        }
        tokenDecoded = false;
      }
    }

    if (
      typeof recivedToken !== 'string' ||
      typeof token2 !== 'string' ||
      recivedToken.length !== token2.length
    ) {
      // This prevents a more subtle timing attack where we know already the tokens aren't going to
      // match but still we don't return fast. Instead we compare two pre-generated random tokens using
      // the same comparison algorithm that we would use to compare two equal-length tokens.
      return {
        expired,
        verified:
          timingSafeEqual(
            Buffer.from(RANDOM_TOKEN_1, 'utf8'),
            Buffer.from(RANDOM_TOKEN_2, 'utf8')
          ) && tokenDecoded,
      };
    }

    return {
      expired,
      verified:
        timingSafeEqual(Buffer.from(recivedToken, 'utf8'), Buffer.from(token2, 'utf8')) &&
        tokenDecoded,
    };
  }
開發者ID:gingerwizard,項目名稱:kibana,代碼行數:43,代碼來源:tokens.ts

示例2: safeCompare

function safeCompare(a: Buffer, b: Buffer): boolean {
  if (a.length !== b.length) {
    return false;
  }

  if (timingSafeEqual) {
    return timingSafeEqual(a, b);
  } else {
    let result = 0;
    for (let i = 0; i < a.length; i++) {
      result |= a[i] ^ b[i];
    }
    return result === 0;
  }
}
開發者ID:nkjm,項目名稱:line-bot-sdk-nodejs,代碼行數:15,代碼來源:validate-signature.ts

示例3: decrypt

export function decrypt(encrypted: string, opts: Options): Either<string, string> {
  const components = encrypted.split('.');
  if (components.length !== 3) return left('Invalid payload');

  setupKeys(opts);

  const iv: Buffer = B.toBuffer(components[0]);
  const ciphertext = B.toBuffer(components[1]);
  const hmac = B.toBuffer(components[2]);

  function cleanup() {
    if (iv) zeroBuffer(iv);

    if (ciphertext) zeroBuffer(ciphertext);

    if (hmac) zeroBuffer(hmac);

    if (expectedHmac) zeroBuffer(expectedHmac);
  }

  // make sure IV is right length
  if (iv.length !== 16) {
    cleanup();
    return left('invalid iv length');
  }

  const expectedHmac = computeHmac(iv, ciphertext, opts);
  if (!timingSafeEqual(hmac, expectedHmac)) {
    cleanup();
    return left('invalid signature');
  }

  const decipher = createDecipheriv(
    opts.encryptionAlgorithm as string,
    opts.encryptionKey,
    iv
  );
  let plaintext = decipher.update(ciphertext, 'binary', 'utf8');
  plaintext += decipher.final('utf8');

  cleanup();
  return right(plaintext);
}
開發者ID:syaiful6,項目名稱:jonggrang,代碼行數:43,代碼來源:encrypt.ts

示例4: Buffer

    const decipher = crypto.createDecipheriv('aes-192-gcm', key, nonce);
    decipher.setAuthTag(tag);
    decipher.setAAD(aad, {
        plaintextLength: ciphertext.length
    });
    const receivedPlaintext: string = decipher.update(ciphertext, null, 'utf8');
    decipher.final();
}

{
    // crypto_timingsafeequal_buffer_test
    const buffer1: Buffer = new Buffer([1, 2, 3, 4, 5]);
    const buffer2: Buffer = new Buffer([1, 2, 3, 4, 5]);
    const buffer3: Buffer = new Buffer([5, 4, 3, 2, 1]);

    assert(crypto.timingSafeEqual(buffer1, buffer2));
    assert(!crypto.timingSafeEqual(buffer1, buffer3));
}

{
    // crypto_timingsafeequal_uint32array_test
    const arr1: Uint32Array = Uint32Array.of(1, 2, 3, 4, 5);
    const arr2: Uint32Array = Uint32Array.of(1, 2, 3, 4, 5);
    const arr3: Uint32Array = Uint32Array.of(5, 4, 3, 2, 1);

    assert(crypto.timingSafeEqual(arr1, arr2));
    assert(!crypto.timingSafeEqual(arr1, arr3));
}

{
    // crypto_timingsafeequal_safe_typedarray_variant_test
開發者ID:SaschaNaz,項目名稱:DefinitelyTyped,代碼行數:31,代碼來源:crypto.ts

示例5: Buffer

        let decipher: crypto.Decipher = crypto.createDecipher("aes-128-ecb", key);
        let decipherBuffers: Buffer[] = [];
        decipherBuffers.push(decipher.update(cipherText));
        decipherBuffers.push(decipher.final());

        let clearText2: Buffer = Buffer.concat(decipherBuffers);

        assert.deepEqual(clearText2, clearText);
    }

    {
      let buffer1: Buffer = new Buffer([1, 2, 3, 4, 5]);
      let buffer2: Buffer = new Buffer([1, 2, 3, 4, 5]);
      let buffer3: Buffer = new Buffer([5, 4, 3, 2, 1]);

      assert(crypto.timingSafeEqual(buffer1, buffer2))
      assert(!crypto.timingSafeEqual(buffer1, buffer3))
    }
}

//////////////////////////////////////////////////
/// TLS tests : http://nodejs.org/api/tls.html ///
//////////////////////////////////////////////////

namespace tls_tests {
    var ctx: tls.SecureContext = tls.createSecureContext({
        key: "NOT REALLY A KEY",
        cert: "SOME CERTIFICATE",
    });
    var blah = ctx.context;
開發者ID:hongshanzhu,項目名稱:DefinitelyTyped,代碼行數:30,代碼來源:node-tests.ts


注:本文中的crypto.timingSafeEqual函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。