check if string is base64

后端 未结 1 762
借酒劲吻你
借酒劲吻你 2021-01-12 05:16

i may recieve these two strings:

    base = Base64.encode64(File.open(\"/home/usr/Desktop/test\", \"rb\").read)
    => \"YQo=\\n\"

    string = File.open         


        
相关标签:
1条回答
  • 2021-01-12 05:59

    You can use something like this, not very performant but you are guaranteed not to get false positives:

    require 'base64'
    
    def base64?(value)
      value.is_a?(String) && Base64.encode64(Base64.decode64(value)) == value
    end
    
    0 讨论(0)
提交回复
热议问题