1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22'use strict';
23// This is the same as test/simple/test-crypto, but from before the shift
24// to use buffers by default.
25
26
27const common = require('../common');
28if (!common.hasCrypto)
29  common.skip('missing crypto');
30
31const assert = require('assert');
32const crypto = require('crypto');
33
34const EXTERN_APEX = 0xFBEE9;
35
36// Manually controlled string for checking binary output
37let ucs2_control = 'a\u0000';
38
39// Grow the strings to proper length
40while (ucs2_control.length <= EXTERN_APEX) {
41  ucs2_control = ucs2_control.repeat(2);
42}
43
44
45// Check resultant buffer and output string
46const b = Buffer.from(ucs2_control + ucs2_control, 'ucs2');
47
48//
49// Test updating from birant data
50//
51{
52  const datum1 = b.slice(700000);
53  const hash1_converted = crypto.createHash('sha1')
54    .update(datum1.toString('base64'), 'base64')
55    .digest('hex');
56  const hash1_direct = crypto.createHash('sha1').update(datum1).digest('hex');
57  assert.strictEqual(hash1_direct, hash1_converted);
58
59  const datum2 = b;
60  const hash2_converted = crypto.createHash('sha1')
61    .update(datum2.toString('base64'), 'base64')
62    .digest('hex');
63  const hash2_direct = crypto.createHash('sha1').update(datum2).digest('hex');
64  assert.strictEqual(hash2_direct, hash2_converted);
65}
66