Local energy generation as well as local energy storage represent key opportunities for energy transition. Nevertheless , their massive deployment is being delayed mainly due to cost reasons. Sharing resources at the local level enables not only reducing these costs significantly, but also to further optimize the cost of the energy exchanged with providers external to the local community. A key question that arises while sharing resources is how to distribute the obtained benefits among the various local players that cooperate. In this paper we propose a cooperative game model, where the players are the holders of energy resources (generation and storage); they cooperate in order to reduce their individual electricity costs. We prove that the core of the game is non-empty; i.e., the proposed cooperative game has a stable solution (distribution of the payoffs among the players) for the case where all players participate in a unique community, and no strict subset of players can obtain a better gain by leaving the community. We propose a formulation of this game, based on the theory of linear production games, which lead us to the two main contributions of this paper. First, we propose an efficient (with linear complexity) centralized algorithm for finding a stable payoff. Second, we provide an efficient distributed algorithm that computes an allocation in the core of the game without any requirement for the players to share any private information. The distributed algorithm requires the exchange of intermediate solutions among players. The topology of the network that enables these exchanges is closely related to the performance of the distributed algorithm. We show, by way of simulations, which are the best topologies for these communication graphs.