js的window.btoa(str) 功能用Python实现

本文介绍了如何使用Python来实现JavaScript中的window.btoa()函数,该函数用于将字符串转换为base-64编码。同时提到了JavaScript的hex_md5加密算法。
摘要由CSDN通过智能技术生成

window.btoa(str)
该方法返回一个 base-64 编码的字符串

下面这一段js代码用Python实现
JavaScript:

var a = '8808';
var timestamp = String(Date.parse(new Date()) / 1000);
var tokens = window.btoa(a + timestamp);

Python:

import time
import base64
timestamp = str(int(time.time()))
a = '8808'
b = base64.encodebytes((a + timestamp).encode("utf8"))
tokens = b.decode("utf8").strip()

另附js的hex_md5加密算法:md5.js

/*
 * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
 * Digest Algorithm, as defined in RFC 1321.
 * Version 2.1 Copyright (C) Paul Johnston 1999 - 2002.
 * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
 * Distributed under the BSD License
 * See http://pajhome.org.uk/crypt/md5 for more info.
 */

/*
 * Configurable variables. You may need to tweak these to be compatible with
 * the server-side, but the defaults work in most cases.
 */
var hexcase = 0;  /* hex output format. 0 - lowercase; 1 - uppercase        */
var b64pad  = ""; /* base-64 pad character. "=" for strict RFC compliance   */
var chrsz   = 8;  /* bits per input character. 8 - ASCII; 16 - Unicode      */

/*
 * These are the functions you'll usually want to call
 * They take string arguments and return either hex or base-64 encoded strings
 */
function hex_md5(s){
    return binl2hex(core_md5(str2binl(s), s.length * chrsz));}
function b64_md5(s){
    return binl2b64(core_md5(str2binl(s), s.length * chrsz));}
function str_md5(s){
    return binl2str(core_md5(str2binl(s), s.length * chrsz));}
function hex_hmac_md5(key, data) {
    return binl2hex(core_hmac_md5(key, data)); }
function b64_hmac_md5(key, data) {
    return binl2b64(core_hmac_md5(key, data)); }
function str_hmac_md5(key, data) {
    return binl2str(core_hmac_md5(key, data)); }

/*
 * Perform a simple self-test to see if the VM is working
 */
function md5_vm_test()
{
   
  return hex_md5("abc") == "900150983cd24fb0d6963f7d28e17f72";
}

/*
 * Calculate the MD5 of an array of little-endian words, and a bit length
 */
function core_md5(x, len)
{
   
  /* append padding */
  x[len >> 5] |= 0x80 << ((len) % 32);
  x[(((len + 64) >>> 9) << 4) + 14] = len;

  var a =  1732584193;
  var b = -271733879;
  var c = -1732584194;
  var d =  271733878;

  for(var i = 0; i < x.length; i += 16)
  {
   
    var olda = a;
    var oldb = b;
    var oldc = c;
    var oldd = d;

    a = md5_ff(a, b, c, d, x[i+ 0], 7 , -680876936);
    d = md5_ff(d, a, b, c, x[i+ 1], 12, -389564586);
    c = md5_ff(c, d, a, b, x[i+ 2], 17,  606105819);
    b = md5_ff(b, c, d, a, x[i+ 3], 22, -1044525330);
    a = md5_ff(a, b, c, d, x[i+ 4], 7 , -176418897);
    d = md5_ff(d, a, b, c, x[i+ 5], 12,  1200080426);
    c = md5_ff(c, d
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值