class SSHKey

def unpacked_byte_array(ssh_type, encoded_key)

def unpacked_byte_array(ssh_type, encoded_key)
  prefix = [ssh_type.length].pack("N") + ssh_type
  decoded = Base64.decode64(encoded_key)
  # Base64 decoding is too permissive, so we should validate if encoding is correct
  unless Base64.encode64(decoded).gsub("\n", "") == encoded_key && decoded.slice!(0, prefix.length) == prefix
    raise PublicKeyError, "validation error"
  end
  byte_count = 0
  data = []
  until decoded.empty?
    front = decoded.slice!(0,4)
    size = front.unpack("N").first
    segment = decoded.slice!(0, size)
    byte_count += segment.length
    unless front.length == 4 && segment.length == size
      raise PublicKeyError, "byte array too short"
    end
    data << OpenSSL::BN.new(segment, 2)
  end
  if ssh_type == "ssh-ed25519"
    unless byte_count == 32
      raise PublicKeyError, "validation error, ed25519 key length not OK"
    end
  end
  return data
end