• 如果用RESTful的风格和你说的这种是不是冲突

    {
        "code": "...",
        "message": "...",
        "details": {}
    }
    
  • 服务器被墙了怎么办 at 2019年09月19日

    看过什么内容会不会有记录

  • 非对称加密,每次加密结果都会不一样

  • 尝试补全go的加密方法

    package main
    import (
        "crypto/rand"
        "crypto/rsa"
        "crypto/x509"
        "encoding/pem"
        "bytes"
        "encoding/base64"
        "fmt"
    )
    
    func SplitSubN(s string, n int) []string {
        sub := ""
        subs := []string{}
    
        runes := bytes.Runes([]byte(s))
        l := len(runes)
        for i, r := range runes {
            sub = sub + string(r)
            if (i + 1) % n == 0 {
                subs = append(subs, sub)
                sub = ""
            } else if (i + 1) == l {
                subs = append(subs, sub)
            }
        }
    
        return subs
    }
    
    func main() {
        data := "hello"
        const publickey = `
    -----BEGIN PUBLIC KEY-----
    MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDw8cUnDShPMYFDZkL65Y0nUyyN
    0Nqxgz6vsFEJj3I3A1nsaQBmGC1urtO9pGJkFzaXkQo7s2y+S53qdsWNufKk8U2l
    SpiGjm4WWCucJTnYpC+nMwAAd/nmQudCtH4czJOkK4m29Tv6fUmvOtcSpQzW7zL0
    47I6qkkAadGb8CAg9wIDAQAB
    -----END PUBLIC KEY-----`
        block, _ := pem.Decode([]byte(publickey))
        pubInterface, _ := x509.ParsePKIXPublicKey(block.Bytes)
        pub := pubInterface.(*rsa.PublicKey)
        partLen := pub.N.BitLen()/8 - 11
        fmt.Println(partLen)
        chunks := SplitSubN(data, partLen)
        buffer := bytes.NewBufferString("")
        for _, chunk := range chunks {
            bytes, err := rsa.EncryptPKCS1v15(rand.Reader, pub, []byte(chunk))
            if err != nil {
                fmt.Println("", err)
            }
            buffer.Write(bytes)
        }
        fmt.Println(base64.RawURLEncoding.EncodeToString(buffer.Bytes()))
    }
    
    #=> "runNVN4ZjB_Z-aA7-7ovGP4bVPNJdKpvXpR47Wn9mdvTYqhUp1z-vN_Q_mGylXH_y423cZyipKnZDEP0dTZFGMfsNhL81NOBNG981pQZ87t9_F2mnyqsoXOR6pUL7j4VQUExMsx3caeMOc_0KkQbjGGnbSvwQ9ewtH7UQBcYiUE"
    

    然后用ruby解密

    private_pem = <<-EOS
    -----BEGIN PRIVATE KEY-----
    MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAPDxxScNKE8xgUNm
    QvrljSdTLI3Q2rGDPq+wUQmPcjcDWexpAGYYLW6u072kYmQXNpeRCjuzbL5Lnep2
    xY258qTxTaVKmIaObhZYK5wlOdikL6czAAB3+eZC50K0fhzMk6Qribb1O/p9Sa86
    1xKlDNbvMvTjsjqqSQBp0ZvwICD3AgMBAAECgYBjForF7JtPNkS6uyuAkzbhnUsN
    Z38I0bjN9+zbyapnBXHNz8DiO78zbaKWsyvjjchdvMBAnSFzgRWIfx/vkTE/59GF
    gfn7VLbvKMYULzbkZbh8NcwNjJLfLtNXmqt973uSiNah9JTeCHQd062VI5D64QHv
    GgKMDAJ6Z2W4jSn8gQJBAPuXT0597Z3ciY6G0EHAhHrJNieDNJboUFodQlHqpTrs
    SLmGTJVswj3Su2vi7ZEPyErHjL2vdrs1hDXKFVik+rkCQQD1KrKA7xUdYRwGnIry
    CMH+uaECSmKhnZgFVg9zCSpDEaTOjenGGVuu6sXWmJKj7PZ1pFHsPrzOCSp/3Pk6
    hmEvAkBL22NeFzn9zvDquFsyeAl5vBFH+JdyqLkRcqD8cfM/wN5skcGljBVE7rTz
    4ogepNOPOHuG9dCmwYwLa9Lj11qBAkAafynZq31BtpwbztFphS7KQ++12qo8Q1cJ
    xJpY2XW61+5S8J2Cbn0tM2x8W/MFSnf8kOzVtpYbQnxYY16AR8O7AkEAkHLKWspk
    QFWRUlliBVpJg+GHe9VI7ND3wWIbV4H1kmyt08959LBpg7cZLu06PXUShwtADaE9
    42Ta3K/fSme4Tw==
    -----END PRIVATE KEY-----
    EOS
    
    s = 'KAPye436VwAqMZ6amu9UeODAh-3dhU6KNbcVXmBU-HK_geeHjyktLzOREMvWPnLcJ7mdddPwwHPEBZfaVliCFl0o9IDIUe7JfEHvmfG7limTUyii08O2b1cBu8pAnev3BxUfDJRqqh84-66Qiiur-syQQkAdnzH-wuG3DJ_UPgo'
    rsa = OpenSSL::PKey::RSA.new(private_pem)
    rsa.private_decrypt(Base64.urlsafe_decode64(s))
    

    成功解密出 hello

    然后用ruby加密

    public_pem = <<-EOS
    -----BEGIN PUBLIC KEY-----
    MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDw8cUnDShPMYFDZkL65Y0nUyyN
    0Nqxgz6vsFEJj3I3A1nsaQBmGC1urtO9pGJkFzaXkQo7s2y+S53qdsWNufKk8U2l
    SpiGjm4WWCucJTnYpC+nMwAAd/nmQudCtH4czJOkK4m29Tv6fUmvOtcSpQzW7zL0
    47I6qkkAadGb8CAg9wIDAQAB
    -----END PUBLIC KEY-----
    EOS
    
    data = 'hello'
    rsa = OpenSSL::PKey::RSA.new(public_pem)
    Base64.urlsafe_encode64 rsa.public_encrypt(data)
    

    用上面ruby的解密方法也能解密

    https://golang.org/pkg/crypto/rsa/#EncryptPKCS1v15

    https://ruby-doc.org/stdlib-2.5.1/libdoc/openssl/rdoc/OpenSSL/PKey/RSA.html#method-i-public_encrypt

    https://commondatastorage.googleapis.com/chromium-boringssl-docs/rsa.h.html#RSA_PKCS1_PADDING

    https://stackoverflow.com/questions/47981709/ruby-rsa-public-key-encryption-to-golang

    结论:你写的是对的

  • mongodb,pg 应该都有基于地理位置的查询功能

  • 代码自测程度是指哪些方法被修改了吗?

  • https://github.com/rest-client/rest-client/blob/master/lib/restclient/request.rb#L368

    如果header名是 symbol 格式会调用

    key = key.to_s.split(/_/).map(&:capitalize).join('-')

    可以改成

    heades: { 'appId' => 'xlkb' }

  • 我有个思路是定一个最小的时间间隔,比如每分钟的记录为一个sortset,然后按需统计

    redis = Redis.current
    redis.zincrby("keyword:#{Time.now.strftime('%Y%m%d%H%M')}", 1, 'Ruby')
    
    # 前一个小时的统计
    hour = Time.now - 1.hour
    start_time = hour.beginning_of_hour
    end_time = hour.end_of_hour
    keys = start_time.to_i.step(end_time.to_i, 60).collect { |t| "keyword:#{Time.at(t).strftime('%Y%m%d%H%M')}" }
    redis.zunionstore("keyword:#{start_time.strftime('%Y%m%d%H')}", keys, aggregate: 'sum')
    
    # 前一天的统计
    day = Time.now - 1.day
    start_time = day.beginning_of_day
    end_time = day.end_of_day
    keys = start_time.to_i.step(end_time.to_i, 60 * 60).collect { |t| "keyword:#{Time.at(t).strftime('%Y%m%d%H')}" }
    redis.zunionstore("keyword:#{start_time.strftime('%Y%m%d')}", keys, aggregate: 'sum')
    
    # 近一周top10关键词
    start_time = (Time.now - 1.week).beginning_of_minute
    end_time = Time.now.end_of_minute
    keys = start_time.to_i.step(end_time.to_i, 60).collect { |t| "keyword:#{Time.at(t).strftime('%Y%m%d%H%M')}" }
    redis.zunionstore("keyword:#{start_time.strftime('%Y%m%d%H%M')}-#{end_time.strftime('%Y%m%d%H%M')}", keys, aggregate: 'sum')
    redis.zrevrange("keyword:#{start_time.strftime('%Y%m%d%H%M')}-#{end_time.strftime('%Y%m%d%H%M')}", 0, 9, with_scores: true)
    
  • 平衡度如何保证

To be, or not to be, that is the question.