How to use base-64

Comprehensive base-64 code examples:

How to use base-64.toByteArray:

4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546


  return byteArray
}


function base64ToBytes (str) {
  return base64.toByteArray(base64clean(str))
}


function blitBuffer (src, dst, offset, length) {
  for (var i = 0; i < length; i++) {

How to use base-64.substring:

12
13
14
15
16
17
18
19
20
  base64 += base64Chars[((bytes[i + 1] & 15) << 2) | (bytes[i + 2] >> 6)];
  base64 += base64Chars[bytes[i + 2] & 63];
}

if ((len % 3) === 2) {
  base64 = base64.substring(0, base64.length - 1) + '=';
} else if (len % 3 === 1) {
  base64 = base64.substring(0, base64.length - 2) + '==';
}

How to use base-64.fromByteArray:

3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
  }
}


function base64Slice (buf, start, end) {
  if (start === 0 && end === buf.length) {
    return base64.fromByteArray(buf)
  } else {
    return base64.fromByteArray(buf.slice(start, end))
  }
}

How to use base-64.encode:

430
431
432
433
434
435
436
437
438
439
		res.redirect('/login_dialog');
	}
	else
	{
		passport.authenticate(authStrategy, {
			state : base64.encode(JSON.stringify(state))
		}
		)(req, res, next);
	}
});

How to use base-64.decode:

456
457
458
459
460
461
462
463
464
465
466
467




app.get('/verifyAsDistributor', async (req, res) => {
  try{
    let orderId = req.query.orderId;
    var bytes = base64.decode(orderId);
    var decodedOrderId = utf8.decode(bytes);
    let o = await Order.findOne({
      orderId : decodedOrderId
    })